开发者

Reset textbox value in javascript

开发者 https://www.devze.com 2023-03-30 02:49 出处:网络
If I have a input textbox like this: <input type=\"text\" id=\"searchField\" name=\"searchField\" />

If I have a input textbox like this:

<input type="text" id="searchField" name="searchField" />

How can I set the value of the textfield using javascript or jQuery?

You would think this was simple but I've tried the following:

Using defaultvalue

var a = document.getElementById("searchField");
a.value = a.defaultValue;

Using jQuery

jQuery("#searchField").focus( function()
{ 
  $(this).val(""); 
} );

Using js

document.getElementById("searchField").value = "";
开发者_开发问答

None of them are doing it... :/


In Javascript :

document.getElementById('searchField').value = '';

In jQuery :

$('#searchField').val('');

That should do it


With jQuery, I've found that sometimes using val to clear the value of a textbox has no effect, in those situations I've found that using attr does the job

$('#searchField').attr("value", "");


Use it like this:

$("#searchField").focus(function() {
    $(this).val("");
});

It has to work. Otherwise it probably never gets focused.


To set value

 $('#searchField').val('your_value');

to retrieve value

$('#searchField').val();


I know this is an old post, but this may help clarify:

$('#searchField')
    .val('')// [property value] e.g. what is visible / will be submitted
    .attr('value', '');// [attribute value] e.g. <input value="preset" ...

Changing [attribute value] has no effect if there is a [property value]. (user || js altered input)


Try using this:

$('#searchField').val('');


First, select the element. You can usually use the ID like this:

$("#searchField"); // select element by using "#someid"

Then, to set the value, use .val("something") as in:

$("#searchField").val("something"); // set the value

Note that you should only run this code when the element is available. The usual way to do this is:

$(document).ready(function() { // execute when everything is loaded
    $("#searchField").val("something"); // set the value
});


This worked for me:

$("#searchField").focus(function()
{ 
    this.value = ''; 
});


this is might be a possible solution

void 0 != document.getElementById("ad") &&       (document.getElementById("ad").onclick =function(){
 var a = $("#client_id").val();
 var b = $("#contact").val();
 var c = $("#message").val();
 var Qdata = { client_id: a, contact:b, message:c }
 var respo='';
     $("#message").html('');

return $.ajax({

    url: applicationPath ,
    type: "POST",
    data: Qdata,
    success: function(e) {
         $("#mcg").html("msg send successfully");

    }

})

});

0

精彩评论

暂无评论...
验证码 换一张
取 消