Check if checkbox is checked with jQuery

后端 未结 23 3480
忘了有多久
忘了有多久 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"));
    }
    
    
    
    
    

提交回复
热议问题