2

所以,我有这个包含我选中的复选框的下一个变量:

var checked = $('input[name="article[]"]:checked');

但是我怎么知道是否有 1 个已选中、2 个或什至没有?因为console.log(checked.length)总是返回 1。

4

3 回答 3

3

I know it's a stupid mistake but for everybody to know, the problem was that I was doing this:

if(checked.length = 1) {

instead of this

if(checked.length == 1) {

btw, I don't know why the "hate" on the other answers (for those who down voted), the answers given... work (I've tested 2 of them)

于 2012-07-19T14:29:13.193 回答
0

尝试

$('input[name=article\\[\\]]:checked').length

见jsfiddle:http: //jsfiddle.net/dyrD5/

于 2012-07-19T14:18:39.863 回答
-1

谁点击了代码悖论的答案?右选择器,只需要长度

<input type="checkbox" name="article" value="1" />
<input type="checkbox" name="article" value="2" />
<input type="checkbox" name="article" value="3" />
<button>go</button>

​ js

$(document).ready(function(){
    $('button').click(function(){
        alert($('input[name^="article"]:checked').length)
    })
});​

在这里工作http://jsfiddle.net/joevallender/GLjTg/

于 2012-07-19T14:27:19.827 回答