Check if checkbox is checked with jQuery

后端 未结 23 3510
忘了有多久
忘了有多久 2020-11-21 23:12

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array?

I am using the following code, but it always returns the count of ch

23条回答
  •  感动是毒
    2020-11-21 23:42

    As per the jQuery documentation there are following ways to check if a checkbox is checked or not. Lets consider a checkbox for example (Check Working jsfiddle with all examples)

    
    

    Example 1 - With checked

    $("#test-with-checked").on("click", function(){
        if(mycheckbox.checked) {
            alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Example 2 - With jQuery is, NOTE - :checked

    var check;
    $("#test-with-is").on("click", function(){
        check = $("#mycheckbox").is(":checked");
        if(check) {
            alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Example 3 - With jQuery prop

    var check;
    $("#test-with-prop").on("click", function(){
        check = $("#mycheckbox").prop("checked");
        if(check) {
             alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Check Working jsfiddle

提交回复
热议问题