您根本不需要each
,jQuery 会为您处理将更改应用于所有匹配的元素:
$(row).children('td > select').val('0');
请注意,设置选择框的值,例如,使具有匹配的选项被value
选中。它与设置所选选项的索引不同(此处的另一个答案向您展示了如何做到这一点)。使用具有非数字值的示例可以使区别更加清晰:
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调用将这两个都更改为'wilma':
$('#characters select').val('wilma');
...或者(对于该特定示例可能更明智)根本没有选择任何选项:
$('#characters select').val('');