Javascript 在javascript中重置文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7174265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset textbox value in javascript
提问by ingh.am
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?
如何使用 javascript 或 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
jQuery("#searchField").focus( function()
{
$(this).val("");
} );
Using js
使用js
document.getElementById("searchField").value = "";
None of them are doing it... :/
他们都没有这样做......:/
回答by David Laberge
In Javascript :
在 JavaScript 中:
document.getElementById('searchField').value = '';
In jQuery :
在 jQuery 中:
$('#searchField').val('');
That should do it
那应该这样做
回答by ingh.am
回答by lukad
Use it like this:
像这样使用它:
$("#searchField").focus(function() {
$(this).val("");
});
It has to work. Otherwise it probably never gets focused.
它必须工作。否则它可能永远不会集中。
回答by Senad Me?kin
To set value
设定值
$('#searchField').val('your_value');
to retrieve value
取回价值
$('#searchField').val();
回答by AnOldMan
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)
如果存在[属性值],则更改[属性值] 无效。(用户 || js 更改了输入)
回答by salar
Try using this:
尝试使用这个:
$('#searchField').val('');
回答by Jose
This worked for me:
这对我有用:
$("#searchField").focus(function()
{
this.value = '';
});
回答by pimvdb
First, select the element. You can usually use the ID like this:
首先,选择元素。您通常可以像这样使用 ID:
$("#searchField"); // select element by using "#someid"
Then, to set the value, use .val("something")
as in:
然后,要设置该值,请使用.val("something")
:
$("#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
});
回答by Malik Muhammad Bilal Awan
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");
}
})
});
});