如何检查特定复选框是否已选中

时间:2016-07-28 13:26:30

标签: jquery html

我有以下代码,我需要检查是否选中了前两个复选框。

我需要检查他们的身份证。

比如如何检查是否检查了前两个复选框。

<input type="checkbox" name="check1" id="check1id" tabindex="1" value="Test" />

<input type="checkbox" name="check2" id="check2id" tabindex="2" value="Test" />

<input type="checkbox" name="check3" id="check3id" tabindex="3" value="Test" />



var boxes = $('.checkbox input');
                if(boxes.length > 0) {
                    if( $(':checkbox:checked').length < 1) {
                        if($("#error-select").length == 0){
                            $("#error-checkbox").show();

                        }
                        return false;
                    }
                    else{
                        $("#error-checkbox").hide();
                    }

如何做到这一点。

此致

2 个答案:

答案 0 :(得分:0)

要查找是否选中了复选框,您可以使用jquery is()选择器

if($("#check1id").is(':checked') || $("#check2id").is(':checked'))
{
alert("You have checked the checkbox check1id")
}

答案 1 :(得分:0)

您可以使用jQuery prop()查找是否选中了复选框。

&#13;
&#13;
$('input[type="checkbox"]').on('click', function() {

  if ($('#check1id').prop('checked') == true && $('#check2id').prop('checked') == true) {
    
    console.log('check1id && check2id are checked');
    
  } else {
    
    console.log('both check1id & check2id are not checked');
    
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="check1" id="check1id" tabindex="1" value="Test" />

<input type="checkbox" name="check2" id="check2id" tabindex="2" value="Test" />

<input type="checkbox" name="check3" id="check3id" tabindex="3" value="Test" />
&#13;
&#13;
&#13;