javascript 单击按钮清除输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25502206/
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
Clear Input Fields on button click
提问by littleanimboy
I have got a input field where in user enters data. There are 2 buttons next to it upon click any one of them or both the input fields get reset. Now since these buttons and field is not inside a form I can't go for a instead how can i clear it with JQuery. The point is, now I have displayed only 2 buttons, the JQuery must also work if there where more buttons..Fiddle link below and the code i tried
我有一个输入字段,用户可以在其中输入数据。单击其中任何一个或两个输入字段都会重置时,它旁边有 2 个按钮。现在,由于这些按钮和字段不在表单内,因此我无法使用 JQuery 清除它。关键是,现在我只显示了 2 个按钮,如果有更多按钮,JQuery 也必须工作......下面的 Fiddle 链接和我试过的代码
$('button').each(function(){
$('button').click(function(){
find('input[type="text"]').val() = "0";
});
});
[Fiddle link] http://jsfiddle.net/vineetgnair/1s22gcL5/
[小提琴链接] http://jsfiddle.net/vineetgair/1s22gcL5/
Thanks to all for help.
感谢大家的帮助。
回答by Kartikeya Khosla
This will work:
这将起作用:
$('button').click(function(){
$('input[type="text"]').val(0);
});
If you want to just reset field then :
如果您只想重置字段,则:
$('button').click(function(){
$('input[type="text"]').val('');
});
回答by Mritunjay
回答by mk117
Instead of javascript variable defining to jQuery.function = 0, you should just reset the value to clear the contents of the input field with .val('');
而不是定义为 jQuery.function = 0 的 javascript 变量,您应该只重置该值以清除输入字段的内容 .val('');
$('button').each(function(){
$('button').click(function(){
$('input[type="text"]').val('');
});
});
回答by Klapsius
This clean text field:
这个干净的文本字段:
$('button').click(function(){
$('input[type="text"]').val("");
});
This change value to 0:
将值更改为 0:
$('button').click(function(){
$('input[type="text"]').val(0);
});
回答by Punitha Subramani Yoganyaa S
<input type="reset" value="button 2" />
回答by RGS
Try this.
试试这个。
$('button').click(function(){
$('input[type="text"]').val('');
});
回答by VPK
You can use following code to clear your input field on button click.
您可以使用以下代码在单击按钮时清除输入字段。
$('button').each(function(){
$(this).click(function(){
$('input[type="text"]').val('');
});
});
回答by Viswanath D
Here'e the code:
这是代码:
$('button').click(function(){
$('input[type="text"]').val('0');
});
That's it.
而已。