jquery使用显示样式和选中状态选择单选按钮

时间:2014-04-02 05:03:43

标签: jquery radiobuttonlist

下面,javascript函数testit2()将显示当您单击“测试”按钮时,div1 div中是否有四组单选按钮中的每一个都有选择。但是,如果其中一个组的显示样式为“none”,则它会失败,因为我无法从该组中进行选择。如何添加按钮必须可见(即显示样式为内联或可见性样式可见)。

<script type="text/javascript">
function testit2()
{
    var radioscomplete = true;
    $('input:radio', $('#div1')).each
    (
        function()
        {
            name = $(this).attr('name');
            if(! $('input:radio[name="' + name + '"]:checked').length)
            {
                alert('Oops, you missed some input there.. [' + name + ']');
                radioscomplete = false;
                return false;
            }
        }
    );
    if (radioscomplete)
        alert("All checked");
}


</script>
</head>
<body>
<div id="div1">
<input type="radio" name="xyz">test1<br />
<input type="radio" name="xyz">test4<br />
<input type="radio" name="xyz1">1test1<br />
<input type="radio" name="xyz1">1test4<br />
<input type="radio" name="xyz2">2test1<br />
<input type="radio" name="xyz2">2test4<br />
<span style="display:inline;"><input type="radio" name="xyz3">3test4<br /></span>

<br>
<button onclick="testit2();">Test</button>
</div>

1 个答案:

答案 0 :(得分:0)

你的意思是

$('input:radio:visible')

或使用

.filter(':visible:checked')