我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
当前回答
我在这里看到的代码和相关的SO问题似乎不完整。
重置表单意味着从HTML中设置原始值,所以我根据上面的代码将其放在一个小项目中:
$(':input', this)
.not(':button, :submit, :reset, :hidden')
.each(function(i,e) {
$(e).val($(e).attr('value') || '')
.prop('checked', false)
.prop('selected', false)
})
$('option[selected]', this).prop('selected', true)
$('input[checked]', this).prop('checked', true)
$('textarea', this).each(function(i,e) { $(e).val($(e).html()) })
如果我遗漏了什么或者有什么需要改进的地方,请告诉我。
其他回答
让我们说,如果你想清除字段,除了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';
}
});
这不会处理表单输入字段默认值为非空的情况。
应该可以工作
$('yourdiv').find('form')[0].reset();
经过测试和验证的代码:
$( document ).ready(function() {
$('#messageForm').submit(function(e){
e.preventDefault();
});
$('#send').click(function(e){
$("#messageForm")[0].reset();
});
});
Javascript必须包含在$(document)中。准备好了,而且必须符合你的逻辑。
$(document).ready(function() {
$('#reset').click(function() {
$('#compose_form').find("input[type=text] , textarea ").each(function() {
$(this).val('');
});
});
});
有什么理由不应该使用它吗?
$("#form").trigger('reset');