admin管理员组文章数量:1301523
Is it possible to reset multiple form fields in one line, or one hit, with jQuery. Note: I don't want to reset all form fields, only a specified whitelist (as below):
// reset some form fields
$('#address11').val('');
$('#address21').val('');
$('#town1').val('');
$('#county1').val('');
$('#postcode1').val('');
Is it possible to reset multiple form fields in one line, or one hit, with jQuery. Note: I don't want to reset all form fields, only a specified whitelist (as below):
// reset some form fields
$('#address11').val('');
$('#address21').val('');
$('#town1').val('');
$('#county1').val('');
$('#postcode1').val('');
Share
Improve this question
asked Oct 25, 2012 at 14:27
crmpiccocrmpicco
17.2k31 gold badges138 silver badges219 bronze badges
1
- Not sure why your question was downvoted... +1 – Cecchi Commented Oct 25, 2012 at 14:32
4 Answers
Reset to default 12It is better to use a class so you do not have to maintain a long list of ids.
HTML
<input type="text" class="resetThis" id="address11" />
<input type="text" class="resetThis" id="address21" />
JavaScript
$(".resetThis").val("");
jQuery (and CSS) selector strings can contain multiple selectors using a comma as a delimiter for sub-selectors:
$('#address11, #address21, #town1, #county1, #postcode1').val('');
I'd argue that this is faster than using a class (ID look-ups should perform in essentially constant time, whereas a class look-up will have to visit every DOM node), but perhaps less maintainable if you're going to want to change which elements get reset.
If you have a lot of fields i'd label the ones you want to ignore with a class to minimise code:
$('#myForm input:not(.ignore)').val('');
You can use $('#Form_name select:not(.fixed)').val('');
本文标签: javascriptreset value of multiple (but not all) form fields with jQuery in one lineStack Overflow
版权声明:本文标题:javascript - reset value of multiple (but not all) form fields with jQuery in one line - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738518406a2091254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论