jQuery - 循环子元素
我在表格中有一系列选择框,如下所示:
<tr>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
</tr>
大约有 10 行。
我正在尝试将行中的所有选择框重置为默认值,但在语法上遇到问题,任何人都可以帮忙吗?这就是我目前所拥有的,但它似乎不起作用:
$(row).children('td > select').each().val('0');
任何建议表示赞赏。
谢谢。
I have a series of select boxes within a table like so:
<tr>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
<td><select></select></td>
</tr>
With about 10 rows.
I'm trying to reset all of the select boxes in the row to a default value, but having trouble with the syntax, can anyone help? This is what I have at the moment, but it dosen't seem to be working:
$(row).children('td > select').each().val('0');
Any advice appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试简单地选择默认值,这应该可行(未经测试):
使用 $('select', row) 比您拥有的选择器要快一些,因此直接访问对象属性,而不是使用jQuery 对象。
If you are trying to simply select a default value, this should work (not tested):
Using $('select', row) is a bit faster than the selector you have, so is accessing directly the objects property, instead of using the jQuery object.
您根本不需要
each
,jQuery 会为您处理将更改应用到所有匹配元素:请注意,这设置了选择框的 值,例如,使已选择匹配
值
的选项。它与设置所选选项的索引不同(此处的另一个答案向您展示了如何做到这一点)。使用非数字值的示例可以使区别更加清晰:HTML:
jQuery call to change those to 'wilma':
...or (对于这个特定示例来说可能更明智)根本没有选择任何选项:
You don't need
each
at all, jQuery handles applying the change to all matched elements for you:Note that that sets the value of the select box, e.g., makes the option with the matching
value
selected. It's not the same as setting the index of the selected option (another of the answers here shows you how to do that). Using an example with non-numeric values makes the distinction a bit clearer:HTML:
jQuery call to change both of those to 'wilma':
...or (perhaps more sensibly for that particular example) to having no options selected at all: