jQuery在提交文本和复选框后清除表单字段
问题描述:
我想在提交后清除表单字段(表单字段具有文本和复选框).为了清除表格,我创建了一个按钮.还有一个单独的提交按钮.
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="">
我已经编写了jQuery代码,但是它不起作用:
I have written jQuery code but its not working:
jQuery("#clearform").click(function(){
jQuery("#submit input[type='text'], input[type='checkbox']").each(function() {
this.value = '';
});
});
答
尝试一下:
$('#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.
希望有帮助.