设置使用jquery选择的第一个选项

时间:2012-11-23 22:27:40

标签: jquery

有两个名称不同的选项列表,我需要设置两个选项中的第一个选项。

<input type="radio" name="first_list" value="0">abc
<input type="radio" name="first_list" value="1">cba

<input type="radio" name="second_list" value="0">opc
<input type="radio" name="second_list" value="1">cpo

当然,我可以这样做:

$("input:radio[name='first_list'][value='0']").attr("checked", "checked");
$("input:radio[name='second_list'][value='0']").attr("checked", "checked");

也许还有另一种更紧凑的方法可以做到这一点?

4 个答案:

答案 0 :(得分:2)

$("input:radio[value='0']").attr("checked", "checked");​

这对我有用:http://jsfiddle.net/jcolicchio/46WXn/

答案 1 :(得分:0)

如果上面显示的标记已完成,您可以使用prop复选框 -

$('input:radio:even').prop('checked', true);

http://jsfiddle.net/r82RE/

答案 2 :(得分:0)

试试这个,无论你在每个集上有多少输入,即使各集之间的值不一致,它也会有效:

$( $( 'input[type=radio]' ).toArray().reverse() ).prop( 'checked', true );​​​​​​​​​​​​​

它实际上会检查从最后到第一个的所有无线电输入,但只有第一个仍将被检查。

答案 3 :(得分:0)

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

    $(document).ready(function () {
        $('input:radio[value="0"]').prop("checked",true)

    });
</script>
</head>
<body>
 <input type="radio" name="first_list" value="0">abc
<input type="radio" name="first_list" value="1">cba

<input type="radio" name="second_list" value="0">opc
<input type="radio" name="second_list" value="1">cpo</body>
</html>