用JQuery检查第一个单选按钮
我想检查每个组的第一个单选按钮。但是有一些单选按钮被禁用,所以脚本应该忽略它们并转到下一个非禁用按钮。用JQuery检查第一个单选按钮
我写了这样的事情,但它不工作:
$(document).ready(function(){ if ($("input['radio']).is(':disabled')) { $(this).attr('checked', false); } else { $(this).attr('checked', true); } });
非常感谢
如果你的按钮被他们的名字属性进行分组,请尝试:
$("input:radio[name=groupName][disabled=false]:first").attr('checked', true);
如果他们
$("#parentId input:radio[disabled=false]:first").attr('checked', true);
如果您想要更改事件触发,请执行'.click()'而不是 – Yarin 2013-09-22 23:51:36
试试这个:
$("input:radio:not(:disabled)").attr("checked", true);
要只在一组选中第一个单选按钮你可以
$("parentelement input:radio:not(:disabled):first-child").attr("checked", true);
我相信OP的意思是“组”,就像“共享相同名称属性的所有单选按钮” – 2010-10-20 12:32:43
“第一个孩子”是不对的 – sje397 2010-10-20 12:38:22
$("input:radio[name=groupX]:not(:disabled):first")
这应该给你从一组的第一个非残疾人单选按钮...
<input type="radio" name="r1"><br>
<input type="radio" name="r1"><br>
<hr>
<input type="radio" name="r2"><br>
<input type="radio" name="r2"><br>
<input type="radio" name="r2"><br>
<script>
$(function(){
//removed duplicates form the following array
$(jQuery.unique(
//create an array with the names of the groups
$('INPUT:radio')
.map(function(i,e){
return $(e).attr('name') }
).get()
))
//interate the array (array with unique names of groups)
.each(function(i,e){
//make the first radio-button of each group checked
$('INPUT:radio[name="'+e+'"]:visible:first')
.attr('checked','checked');
});
});
</script>
:由一个父容器,然后被分组210
jsfiddle = http://jsfiddle.net/v4auT/
Nice Solution(y) – 2016-04-01 08:16:25
input [type = radio] instead – 2010-10-20 12:24:25
**或**,@Haim,'input:radio'。 – 2010-10-20 12:30:08