我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
当前回答
下面的代码清除所有表单,它的字段将为空。如果页面有多个表单,而您只想清除特定的表单,请注明表单的id或类
$("body").find('form').find('input, textarea').val('');
其他回答
$(".reset").click(function() {
$(this).closest('form').find("input[type=text], textarea").val("");
});
最简单和最好的解决方案是- $(" #形式”)[0].reset ();
不要在这里用- $ () [0] .reset ();
添加隐藏复位按钮如下
<input id="resetBtn" type="reset" value="reset" style="display:none" />
// Call reset buttons click event
// Similar to ClearInputs('resetBtn');
function ClearInputs(btnSelector) {
var btn = $("#" + btnSelector);
btn.click();
}
对于jQuery 1.6+:
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.prop('checked', false)
.prop('selected', false);
对于jQuery < 1.6:
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
请看这篇文章: 使用jQuery重置多级表单
Or
$('#myform')[0].reset();
jQuery建议:
要检索和更改DOM属性,例如表单元素的选中、选中或禁用状态,请使用.prop()方法。
如果我想清除除accountType以外的所有字段,请使用以下方法
$q(':input','#myform').not('#accountType').val('').removeAttr('checked').removeAttr('selected');