Jquery 如何选择每个组的指定单选按钮
我有这个HTML:Jquery 如何选择每个组的指定单选按钮,jquery,Jquery,我有这个HTML: <fieldset class="target-radios"> <legend>Date Range</legend> <div> <input id="dateUnspecified" type="radio" name="muScheduleDateRange" value="0" selected-item="0"> </div>
<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>
我不太清楚你的意思,但你可以尝试类似的方法
$('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”相同的类,这样您就可以使用
$('.group')。each()
。我不太清楚您的意思,但您可以尝试类似的方法
$('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”相同的类,这样您就可以使用
$('.group')。each()
。尽量不要构成自己的属性。我将数据选定项
替换为您的选定项
$('div').each(function () {
$('input', this).eq($(this).find('input').data('selected-item') - 1).prop('checked', true);
});
更新:
您可以根据您的更改尝试此变体:
$('input').each(function () {
if ($(this).data('selected-item') == (parseInt($(this).val(), 10))) $(this).prop('checked', true);
});
尽量不要编造自己的属性。我将
数据选定项
替换为您的选定项
$('div').each(function () {
$('input', this).eq($(this).find('input').data('selected-item') - 1).prop('checked', true);
});
更新:
您可以根据您的更改尝试此变体:
$('input').each(function () {
if ($(this).data('selected-item') == (parseInt($(this).val(), 10))) $(this).prop('checked', true);
});
您必须记住元素的索引为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...
编辑重新:更新的标记:删除a选择器上的-1
$('.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索引的,所以减去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...
编辑重新:更新的标记:删除a选择器上的-1
$('.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);
}
});
工作
试试这个,我已经添加了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);
}
});
希望这有帮助,谢谢你的工作
试试这个,我已经添加了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);
}
});
希望这有帮助,谢谢你
给你
这将使收音机具有不同的'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);
}
var divs=$('input').map(函数(){
返回此名称
}).get();
var uniqueName=$.unique(divs);
对于(i=0;i
给你
这将使收音机具有不同的'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);
}
var divs=$('input').map(函数(){
返回此名称
}).get();
var uniqueName=$.unique(divs);
对于(i=0;我能解释一下你所说的“指定”是什么意思吗?如果成功了,你希望尝试做什么?你能解释一下你所说的“指定”是什么意思吗?如果成功了,你希望尝试做什么?我想选择第一组中的第二个单选按钮和第二组中的第三个单选按钮。很简单!@RuntimeException我想我刚刚理解了你想要什么t、 我已经更新了我的代码,检查它是否适合你。我想选择第1组中的第2个单选按钮和第2组中的第3个单选按钮。很简单!@RuntimeException我想我刚刚了解你想要什么。我已经更新了我的代码,检查它是否适合你。效果很好,但是我对我的html结构有一个问题,你能在没有div的情况下让它正常工作吗?有什么问题吗如何使其与当前结构一起工作?谢谢帮助。如果您不不断更改HTML,这将非常有帮助。现在在这个最新的示例中,将重复什么?字段集块或字段集中的div?为更改HTML表示歉意。字段集块将重复。效果很好,但是我的HTML结构c有问题你能在没有div的情况下完成这项工作吗?有没有办法让它在当前结构下工作?谢谢你的帮助。如果你不一直更改HTML,它会非常有用。现在在这个最新的示例中,会重复什么?字段集块或字段集中的div?为更改HTML表示歉意。字段集块将重复。谢谢rk,它选择了错误的(第三)按钮,更新了标记,有什么线索吗?@RuntimeException-我添加了一个示例,删除了-1。谢谢马克,它选择了错误的(第三)按钮带有更新标记的按钮,有任何线索吗?@RuntimeException-我添加了一个示例,删除了-1。我添加了另一个答案,请检查,我认为它符合您的要求。我添加了另一个答案,请检查,我认为它符合您的要求