-1

我想检查是否在 Jquery 中检查了特定答案

<label><input type="checkbox" class="q1" name="q1" id="q1-a" />aaa</label>
<label><input type="checkbox" class="q1" name="q1" id="q1-b" />bbb</label>
<label><input type="checkbox" class="q1" name="q1" id="q1-c" />ccc</label>
<label><input type="checkbox" class="q1" name="q1" id="q1-d" />ddd</label>

我有 jquery 代码来检查盒装检查的数量,但很想知道是否检查了 1 和 2 选项

$('input[type=checkbox]:checked:visible').length == 2
4

3 回答 3

0

你可以试试这样的

// At first get all the checked items
var checked = $('.q1:checked').get();

// then loop through the array and check their ids
if(checked) {
    $.each(checked, function(){
        console.log(this.id); // q1-a, q1-b, q1-c, q1-d (if checked)
    });
}

检查这个小提琴。也许还有其他简单的方法,但不确定您要做什么。

更新:你可以试试这个

$('.btnNext1').on('click', function(){
    if( $('#q1-a').is(':checked') || $('#q1-b').is(':checked') ) {
        return false;
    }

    // do whatever you want to do when one of those are not checked
});

检查这个小提琴。

于 2013-11-10T16:56:38.463 回答
0
$('#q1-a').is(':checked');
$('#q1-b').is(':checked');
etc.
于 2013-11-10T16:37:20.997 回答
0

只需删除长度,您将获得所选元素的数组:

var checked = $('input[type=checkbox]:checked:visible'); //array of checked checkboxes
于 2013-11-10T16:44:01.070 回答