jQuery: Test if checkbox is NOT checked

前端 未结 18 922
太阳男子
太阳男子 2020-12-02 07:12

I\'m having trouble figuring this out. I have two checkboxes (in the future will have more):

  • checkSurfaceEnvironment-1
  • checkSurface
相关标签:
18条回答
  • 2020-12-02 08:08

    I was looking for a more direct implementation like avijendr suggested.

    I had a little trouble with his/her syntax, but I got this to work:

    if ($('.user-forms input[id*="chkPrint"]:not(:checked)').length > 0)
    

    In my case, I had a table with a class user-forms, and I was checking if any of the checkboxes that had the string checkPrint in their id were unchecked.

    0 讨论(0)
  • 2020-12-02 08:09

    An alternative way:

    Here is a working example and here is the code, you should also use prop.

    $('input[type="checkbox"]').mouseenter(function() { 
        if ($(this).is(':checked')) {
            //$(this).prop('checked',false);
            alert("is checked");
        } else {
             //$(this).prop('checked',true);
            alert("not checked");
        }
    });​
    

    I commented out the way to toggle the checked attribute.

    0 讨论(0)
  • 2020-12-02 08:10
    if($("#checkbox1").prop('checked') == false){
        alert('checkbox is not checked');
        //do something
    }
    else
    { 
        alert('checkbox is checked');
    }
    
    0 讨论(0)
  • 2020-12-02 08:11

    You can also use either jQuery .not() method or :not() selector:

    if ($('#checkSurfaceEnvironment').not(':checked').length) {
        // do stuff for not selected
    }
    

    JSFiddle Example


    Additional Notes

    From the jQuery API documentation for the :not() selector:

    The .not() method will end up providing you with more readable selections than pushing complex selectors or variables into a :not() selector filter. In most cases, it is a better choice.

    0 讨论(0)
  • 2020-12-02 08:12

    Here I have a snippet for this question.

    $(function(){
       $("#buttoncheck").click(function(){
            if($('[type="checkbox"]').is(":checked")){
                $('.checkboxStatus').html("Congratulations! "+$('[type="checkbox"]:checked').length+" checkbox checked");
            }else{
                $('.checkboxStatus').html("Sorry! Checkbox is not checked");
             }
             return false;
       })
        
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form>
      <p>
        <label>
          <input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0">
          Checkbox</label>
        <br>
        <label>
          <input type="checkbox" name="CheckboxGroup1_" value="checkbox" id="CheckboxGroup1_1">
          Checkbox</label>
        <br>
      </p>
      <p>
        <input type="reset" value="Reset">
        <input type="submit" id="buttoncheck" value="Check">
      </p>
      <p class="checkboxStatus"></p>
    </form>

    0 讨论(0)
  • 2020-12-02 08:13

    I know this has already been answered, but still, this is a good way to do it:

    if ($("#checkbox").is(":checked")==false) {
        //Do stuff here like: $(".span").html("<span>Lorem</span>");
    }
    
    0 讨论(0)
提交回复
热议问题