jQuery 在提交文本和复选框后清除表单字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26479557/
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
jQuery to clear form fields after submit for text and checkbox
提问by user2201935
I want to clear the form fields (form fields has text and checkbox) after submit. To clear the form, I have created a button. And there is separate button for submit.
我想在提交后清除表单域(表单域有文本和复选框)。为了清除表单,我创建了一个按钮。并且有单独的提交按钮。
<input type="reset" name="clearform" id="clearform" value="Clear Form" />
<form id="submit" method="POST" action="">
I have written jQuery code but its not working:
我已经编写了 jQuery 代码,但它不起作用:
jQuery("#clearform").click(function(){
jQuery("#submit input[type='text'], input[type='checkbox']").each(function() {
this.value = '';
});
});
采纳答案by MysticMagic?
Try this:
尝试这个:
$('#clearform').on('click', function () {
$('#form_id').find('input:text').val('');
$('input:checkbox').removeAttr('checked');
});
One will clear all text inputs. Second will help unchecking checkboxes.
一个将清除所有文本输入。其次将有助于取消选中复选框。
Hope it helps.
希望能帮助到你。
回答by Faizan Noor
There is a simple solution to reset form via jQuery:
有一个简单的解决方案可以通过 jQuery 重置表单:
$("form").trigger("reset");
回答by Abdulla Chozhimadathil
try the code shown below to reset a form
尝试使用下面显示的代码重置表单
$('#submit')[0].reset();
回答by Afghan Dev
jQuery has no .reset() method. But native Javascript does!
jQuery 没有 .reset() 方法。但是原生 Javascript 可以!
$("#form").get(0).reset()
// Result:
// A clean, resetted form!
回答by Fergoso
I'd clear fields using a server confirmation response rather than an extra click.
我会使用服务器确认响应而不是额外的点击来清除字段。
$("#submit").submit(function() {
var submit = $(this).serialize();
$.post('serverside.php', submit,
function(data){
if(data == "complete"){ //server response
jQuery("#submit input[type=text]").val('');
jQuery("#submit input[type=checkbox]").prop("checked", false);
};
});
return false;
});
EDIT
编辑
If you want to reset using a button I'd do
如果您想使用按钮重置,我会这样做
$('#clearform').on('click', function () {
$('#submit').trigger("reset");
});