0

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

<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

4 回答 4

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

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

于 2012-11-23T22:31:17.617 回答
0

如果您在上面显示的标记是完整的,您可以使用prop检查框 -

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

http://jsfiddle.net/r82RE/

于 2012-11-23T22:32:26.557 回答
0
于 2012-11-23T22:54:07.767 回答
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>
于 2016-01-21T15:54:20.543 回答