2

我的 HTML:

<input type="checkbox" name="subjects[0]" class="checkbox_array" value="1"  />
<input type="checkbox" name="subjects[1]" class="checkbox_array"  value="1"  />
<input type="checkbox" name="subjects[2]" class="checkbox_array" value="1"  />
<input type="checkbox" name="subjects[3]" class="checkbox_array" value="1"  />
<input type="button" id="ss" />

jQuery:

$("#ss").click(function(){
    if($('input[name=subjects\\[\\]]:checked').length<=0)
    {
        alert("No radio checked")
    }
});

它对我不起作用...使用类if($('.checkbox_array:checked').length<=0) 它很容易但是我如何使用输入名称选择器

4

2 回答 2

6
($('input[name*="subjects"]:checked').length<=0)

Will work...

Doc: http://api.jquery.com/attribute-contains-selector/

于 2013-10-24T13:16:53.883 回答
5

You can do this using the Attribute Starts With Selector:

$("#ss").click(function () {
    if ($('input[name^=subjects]:checked').length <= 0) {
        alert("No radio checked")
    }
});

Demo: Fiddle

于 2013-10-24T13:16:27.417 回答