我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
当前回答
添加隐藏复位按钮如下
<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();
}
其他回答
你们中的一些人抱怨无线电和诸如此类的默认“已检查”状态被清除…你所要做的就是将:radio,:复选框选择器添加到.not中,问题就解决了。
如果你不能让所有其他重置函数工作,这个可以。
改编自ngen的回答 函数form_reset (formID) { $(':输入',' # ' + formID) 另(‘:按钮’,‘:提交’,”:重置”,“:隐藏”,“:广播”,“:复选框”) .val ("); }
让我们说,如果你想清除字段,除了accountType,在同一时间下拉框将被重置为特定的值,i。“所有”。其余字段应重置为空即文本框。这种方法将用于清除我们需要的特定字段。
$(':input').not('#accountType').each( function() {
if(this.type=='text' || this.type=='textarea'){
this.value = '';
}
else if(this.type=='radio' || this.type=='checkbox'){
this.checked=false;
}
else if(this.type=='select-one' || this.type=='select-multiple'){
this.value ='All';
}
});
$('form').submit(function() {
var el = $(this);
$('<button type="reset" style="display:none; "></button>')
.appendTo(el)
.click()
.remove()
;
return false;
});
$('#editPOIForm').each(function(){
this.reset();
});
其中editPOIForm是表单的id属性。
为什么需要用JavaScript来完成呢?
<form>
<!-- snip -->
<input type="reset" value="Reset"/>
</form>
http://www.w3.org/TR/html5/the-input-element.html#attr-input-type-keywords
先尝试一下,它不会清除具有默认值的字段。
下面是一种用jQuery实现的方法:
$('.reset').on('click', function() {
$(this).closest('form').find('input[type=text], textarea').val('');
});