Проверьте первый переключатель с помощью JQuery
Я хочу проверить первый переключатель каждой группы. Но есть некоторые переключатели, которые отключены, поэтому script должен игнорировать их и перейти к следующей не отключенной кнопке.
Я написал что-то вроде этого, но это не работает:
$(document).ready(function(){
if ($("input['radio']).is(':disabled'))
{
$(this).attr('checked', false );
}
else
{
$(this).attr('checked', true );
}
});
Спасибо большое
Ответы
Ответ 1
Если ваши кнопки сгруппированы по их атрибуту имени, попробуйте:
$("input:radio[name=groupName][disabled=false]:first").attr('checked', true);
Если они сгруппированы родительским контейнером, то:
$("#parentId input:radio[disabled=false]:first").attr('checked', true);
Ответ 2
$("input:radio[name=groupX]:not(:disabled):first")
Это должно дать вам первую отключенную отключаемую кнопку от группы...
Ответ 3
Ниже делается то, что вы хотите:
$(document).ready(
function(){
$('input:radio:first-child').attr('checked',true);
}
);
Демо на: JS Bin.
Ответ 4
<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>
jsfiddle = http://jsfiddle.net/v4auT/
Ответ 5
Попробуйте следующее:
$("input:radio:not(:disabled)").attr("checked", true);
Чтобы проверить первый переключатель только в группе, вы можете
$("parentelement input:radio:not(:disabled):first-child").attr("checked", true);
Ответ 6
Я проверил первый ответ, и он не разрешает его. Я должен был использовать следующее предложение:
jQuery("input:radio[name=groupName]:visible")[0].checked=true;
Эта проверка первой видимой радиокнопки с именем = groupName