在一行中用jQuery重置多个(但不是全部)表单字段的值 [英] reset value of multiple (but not all) form fields with jQuery in one line
问题描述
是否可以使用jQuery重置一行或一次命中中的多个表单域。注意:我不想重置所有表单字段,只需指定白名单(如下所示):
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('');
推荐答案
jQuery(和CSS)选择器字符串可以包含多个选择器使用逗号作为子选择符的分隔符:
jQuery (and CSS) selector strings can contain multiple selectors using a comma as a delimiter for sub-selectors:
$('#address11, #address21, #town1, #county1, #postcode1').val('');
我认为这比使用类更快查询应该在基本不变的时间内执行,而类查找将不得不访问每个DOM节点),但如果要更改哪些元素会被重置,则可能无法维护。
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.
这篇关于在一行中用jQuery重置多个(但不是全部)表单字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!