Check if checkbox is checked with jQuery

后端 未结 23 3053
忘了有多久
忘了有多久 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:44

    Your question is not clear: you want to give "checkbox array id" at input and get true/false at output - in this way you will not know which checkbox was checked (as your function name suggest). So below there is my proposition of body of your isCheckedById which on input take checkbox id and on output return true/false (it's very simple but your ID should not be keyword),

    this[id].checked
    

    function isCheckedById(id) {
      return this[id].checked;
    }
    
    
    
    // TEST
    
    function check() {
      console.clear()
      console.log('1',isCheckedById("myCheckbox1"));
      console.log('2',isCheckedById("myCheckbox2"));
      console.log('3',isCheckedById("myCheckbox3"));
    }
    <label><input id="myCheckbox1" type="checkbox">check 1</label>
    <label><input id="myCheckbox2" type="checkbox">check 2</label>
    <label><input id="myCheckbox3" type="checkbox">check 3</label>
    <!-- label around inputs makes text clickable -->
    <br>
    <button onclick="check()">show checked</button>

    0 讨论(0)
  • 2020-11-21 23:47

    For checkbox with an id

    <input id="id_input_checkbox13" type="checkbox"></input>
    

    you can simply do

    $("#id_input_checkbox13").prop('checked')
    

    you will get true or false as return value for above syntax. You can use it in if clause as normal boolean expression.

    0 讨论(0)
  • 2020-11-21 23:47

    Actually, according to jsperf.com, The DOM operations are fastest, then $().prop() followed by $().is()!!

    Here are the syntaxes :

    var checkbox = $('#'+id);
    /* OR var checkbox = $("input[name=checkbox1]"); whichever is best */
    
    /* The DOM way - The fastest */
    if(checkbox[0].checked == true)
       alert('Checkbox is checked!!');
    
    /* Using jQuery .prop() - The second fastest */
    if(checkbox.prop('checked') == true)
       alert('Checkbox is checked!!');
    
    /* Using jQuery .is() - The slowest in the lot */
    if(checkbox.is(':checked') == true)
       alert('Checkbox is checked!!');
    

    I personally prefer .prop(). Unlike .is(), It can also be used to set the value.

    0 讨论(0)
  • 2020-11-21 23:52

    The most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property

    All below methods are possible

    elem.checked 
    
    $(elem).prop("checked") 
    
    $(elem).is(":checked") 
    
    0 讨论(0)
  • 2020-11-21 23:52

    Simple Demo for checking and setting a check box.

    jsfiddle!

    $('.attr-value-name').click(function() {
        if($(this).parent().find('input[type="checkbox"]').is(':checked'))
        {
            $(this).parent().find('input[type="checkbox"]').prop('checked', false);
        }
        else
        {
            $(this).parent().find('input[type="checkbox"]').prop('checked', true);
        }
    });
    
    0 讨论(0)
  • 2020-11-21 23:53

    I know the OP want jquery but in my case pure JS was the answer so if anyone like me is here and do not have jquery or do not want to use it - here is the JS answer:

    document.getElementById("myCheck").checked
    

    It returns true if the input with ID myCheck is checked and false if it is not checked.

    Simple as that.

    0 讨论(0)
提交回复
热议问题