jQuery - 循环子元素

发布于 2024-09-02 02:53:45 字数 473 浏览 0 评论 0原文

我在表格中有一系列选择框,如下所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-09-09 02:53:45

如果您尝试简单地选择默认值,这应该可行(未经测试):

$('select', row).each(function() {
    this.selectedIndex = 0; //or whatever the index you want
});

使用 $('select', row) 比您拥有的选择器要快一些,因此直接访问对象属性,而不是使用jQuery 对象。

If you are trying to simply select a default value, this should work (not tested):

$('select', row).each(function() {
    this.selectedIndex = 0; //or whatever the index you want
});

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.

南城旧梦 2024-09-09 02:53:45

您根本不需要 each,jQuery 会为您处理将更改应用到所有匹配元素:

$(row).children('td > select').val('0');

请注意,这设置了选择框的 ,例如,使已选择匹配的选项。它与设置所选选项的索引不同(此处的另一个答案向您展示了如何做到这一点)。使用非数字值的示例可以使区别更加清晰:

HTML:

<div id='characters'>
<select name='favorite'>
<option value='fred'>Fred Flintstone</option>
<option value='barney'>Barney Rubble</option>
<option value='wilma'>Wilma Flintstone</option>
<option value='betty' selected>Betty Rubble</option>
</select>
<select name='secondchoice'>
<option value='fred'>Fred Flintstone</option>
<option value='barney selected'>Barney Rubble</option>
<option value='wilma'>Wilma Flintstone</option>
<option value='betty'>Betty Rubble</option>
</select>

jQuery call to change those to 'wilma':

$('#characters select').val('wilma');

...or (对于这个特定示例来说可能更明智)根本没有选择任何选项:

$('#characters select').val('');

You don't need each at all, jQuery handles applying the change to all matched elements for you:

$(row).children('td > select').val('0');

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:

<div id='characters'>
<select name='favorite'>
<option value='fred'>Fred Flintstone</option>
<option value='barney'>Barney Rubble</option>
<option value='wilma'>Wilma Flintstone</option>
<option value='betty' selected>Betty Rubble</option>
</select>
<select name='secondchoice'>
<option value='fred'>Fred Flintstone</option>
<option value='barney selected'>Barney Rubble</option>
<option value='wilma'>Wilma Flintstone</option>
<option value='betty'>Betty Rubble</option>
</select>

jQuery call to change both of those to 'wilma':

$('#characters select').val('wilma');

...or (perhaps more sensibly for that particular example) to having no options selected at all:

$('#characters select').val('');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文