使用jQuery清空表单字段
使用jQuery清空表单字段
我想清除一个表单中所有的输入框和文本域。当使用带有reset
类的输入按钮时,它的工作方式如下:
$(".reset").bind("click", function() { $("input[type=text], textarea").val(""); });
这将清除页面上所有的字段,而不仅仅是表单中的字段。我的选择器应该如何才能仅清除实际重置按钮所在的表单中的字段?
admin 更改状态以发布 2023年5月24日
对于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');
请参考以下链接:
或者
$('#myform')[0].reset();
如jQuery 建议:
要检索和更改表单元素的
checked
,selected
或disabled
状态等DOM属性,请使用.prop()方法。