2013-08-27 28 views
0

指定单选按钮我有这样的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); 
    } 
}); 
+0

你能解释一下你的意思吗? – Adil

+0

你认为如果尝试成功,你会做些什么? –

回答

1

你必须记住元素是0索引,所以减1。

$('input[type=radio]').each(function() { 
    var name = $(this).attr('name'), 
     aselected = parseInt($(this).attr('selected-item'), 10) - 1; 
    if (typeof aselected !== 'undefined') { 
     $('input[type="radio"][name="' + name + '"]').eq(aselected).prop('checked', true); 
    } 
}); 

注:显示没有目标,但要补充一点变化:

$('#targeting').find('input[type=radio]').each... 

编辑回复:更新标记:去除-1的aselector指数计算

$('.target-radios').find('input[type=radio]').each(function() { 
    var name = $(this).attr('name'), 
     aselected = parseInt($(this).attr('selected-item'), 10); 
    var me = 'input[type="radio"][name="' + name + '"]'; 
    if (typeof aselected !== 'undefined') { 
     $(me).eq(aselected).prop('checked', true); 
    } 
}); 
+0

感谢马克,它选择错误(第三)按钮与更新的标记,任何线索? – RuntimeException

+0

@RuntimeException - 我添加了一个示例,删除了-1。 –

1

我不太清楚你的意思,但你可以尝试一些与此类似,

$('div').each(function() { 
    // Determine the selected item id 
    var selectedItemId = $(this).eq(0).attr('selected-item'); 

    // Check it 
    $(this).find('input[value="' + selectedItemId + '"]').prop('checked', true); 
}); 

此外,你可以给你的div相同的类如“”,所以你可以使用$('.group').each()来代替。

+0

我想选择第1组中的第2个单选按钮和第2个中的第3个单选按钮。简单! – RuntimeException

+0

@RuntimeException我想我只是明白你想要什么。我更新了我的代码,检查它是否适用于您。 – federicot

1

尽量不要组成自己的属性。我取代data-selected-itemselected-item

$('div').each(function() { 
    $('input', this).eq($(this).find('input').data('selected-item') - 1).prop('checked', true); 
}); 

jsFiddle example

更新:

您可以尝试根据您的变化这种变化:

$('input').each(function() { 
    if ($(this).data('selected-item') == (parseInt($(this).val(), 10))) $(this).prop('checked', true); 
}); 

jsFiddle example

+0

伟大的工程,但我有我的html结构的问题,你可以让这个工作没有div? – RuntimeException

+0

答复已更新。 – j08691

+0

有什么办法可以使它与当前结构一起工作?感谢帮助。 – RuntimeException

1

工作DEMO

试试这个,我已经加了targeting DIV,

$('#targeting div').each(function() { 
    var name = $(this).children(0).attr('name'); 
    var selected = $(this).children(0).attr('selected-item'); 
    if(typeof selected !== 'undefined') { 
     $('input[name='+ name +']:nth-child('+ selected +')').attr('checked', true); 
    } 
}); 

希望这会有所帮助,谢谢

+0

你能否让它使用更新的标记? – RuntimeException

+0

其可能。我会制定并给你 – SarathSprakash

+0

我已经添加了另一个答案,检查出来,我认为它符合你的要求 – SarathSprakash

0

DEMO

这将让具有鲜明'name'属性的电台,所以循环将被执行取决于不同组的数量,即'name'属性

var divs = $('input').map(function() { 
    return this.name 
}).get(); 
var uniqueName = $.unique(divs); 
for(i=0 ; i<uniqueName.length;i++) 
{ 
var selected = $("input[name="+uniqueName[i]+"").attr('selected-item'); 
$('input[name='+ uniqueName[i] +']').eq(selected).attr('checked', true); 
} 

希望这会有所帮助,谢谢

+0

我想这符合你的要求... – SarathSprakash