我有这个 HTML:
<fieldset class="target-radios">
<legend>Date Range</legend>
<div>
<input id="dateUnspecified" type="radio" name="muScheduleDateRange" value="0" selected-item="0">
</div>
<div>
<input id="dateAll" type="radio" name="muScheduleDateRange" value="1" selected-item="0">
</div>
<div class="from-to">
<input type="radio" id="dateFromTo" name="muScheduleDateRange" value="2" selected-item="0">
</div>
</fieldset>
如何通过循环遍历每个组来选择指定的单选按钮(选定项)?理想循环每组只能工作一次。
尝试了这种方法,但不起作用并多次循环遍历每个组:
$('#targeting input:radio').each(function() {
var name = $(this).attr('name'),
selected = $(this).attr('selected-item');
if(typeof selected !== 'undefined') {
$('input[name='+ name +']:nth-child('+ selected +')').attr('checked', true);
}
});