用于jQuery验证的复选框数组选择器

Dha*_*amu 2 html validation jquery

我的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" />
Run Code Online (Sandbox Code Playgroud)

jQuery的:

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

它不适合我...使用类if($('.checkbox_array:checked').length<=0) 它很容易工作,但我怎么能用输入名称选择器

Mar*_*rai 6

($('input[name*="subjects"]:checked').length<=0)
Run Code Online (Sandbox Code Playgroud)

将工作...

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


pal*_*aѕн 5

你可以这样做Attribute Starts With Selector:

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

演示:小提琴