How do I determine if a checkbox is checked?

前端 未结 9 2091
我在风中等你
我在风中等你 2020-11-27 05:23

For some reason, my form does not want to get the value of a checkbox... I am not sure if it is my coding or not, but when I try and alert() the value, I get

相关标签:
9条回答
  • 2020-11-27 06:09

    You can use this code, it can return true or false:

    $(document).ready(function(){
      
      //add selector of your checkbox
    
      var status=$('#IdSelector')[0].checked;
      
      console.log(status);
    
    });

    0 讨论(0)
  • 2020-11-27 06:16

    <!doctype html>
    <html lang="en">
    <head>
    </head>
    <body>
    <label><input class="lifecheck" id="lifecheck" type="checkbox" checked >Lives</label>
    
    <script type="application/javascript" >
        lfckv = document.getElementsByClassName("lifecheck");
        if (true === lfckv[0].checked) {
          alert('the checkbox is checked');
        }
    </script>
    </body>
    </html>

    so after you can add event in javascript to have dynamical event affected with the checkbox .

    thanks

    0 讨论(0)
  • 2020-11-27 06:17

    var elementCheckBox = document.getElementById("IdOfCheckBox");
    
    
    elementCheckBox[0].checked //return true if checked and false if not checked

    thanks

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