重置一个表单,但一个特定字段除外 [英] Reset a form with the exception of one specific field
问题描述
我想清除表格form1
而不清除文本框textbox1
.但是以下方法不起作用:
I want to clear the form form1
without clearing the textbox textbox1
. However the below does not work:
$('#form1').not("input[type=text]#textbox1").trigger('reset');
除了textbox1
之外,我可以对每个字段执行.html('')
,但是我不想这样做,因为会在表单中添加更多字段,因此可操作性不好.
I could do .html('')
for each field except for textbox1
, but I do not want to do that since there will be more fields added to the form and therefore the manitaninability is not good.
推荐答案
不幸的是,reset
事件发生在form
上,并影响其中的所有与表单相关的元素.要实现所需的功能,您可以选择所有必需的表单元素并手动设置其值,例如:
Unfortunately the reset
event occurs on the form
and affects all form related elements within it. To achieve what you need you could select all your required form elements and set their values manually, eg:
$('#form1').find('input, select, textarea').not("#textbox1").val('');
上面的元素选择器应该足够通用,以适应动态添加到表单中的其他字段
The element selectors above should be generic enough to cater for additional fields being added to the form dynamically
这篇关于重置一个表单,但一个特定字段除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!