jQuery: Test if checkbox is NOT checked

前端 未结 18 920
太阳男子
太阳男子 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 07:48
    $("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4").change(function () {
            var item = $("#chkFruits_0,#chkFruits_1,#chkFruits_2,#chkFruits_3,#chkFruits_4");
        if (item.is(":checked")==true) {
            //execute your code here
        }
    
        else if (item.is(":not(:checked)"))
        {
            //execute your code here
        }
    
    });
    
    0 讨论(0)
  • 2020-12-02 07:49

    To do it with .attr() like you have, to see if it's checked it would be .attr("checked", "checked"), and if it isn't it would be .attr("checked") == undefined

    0 讨论(0)
  • 2020-12-02 07:49

    try this one

    if ($("#checkSurfaceEnvironment-1:checked").length>0) {
        //your code in case of NOT checked
    }
    

    In Above code selecting the element by Id (#checkSurfaceEnvironment-1) then filter out it's checked state by (:checked) filter.

    It will return array of checked element object. If there any object exists in the array then if condition will be satisfied.

    0 讨论(0)
  • 2020-12-02 07:52

    I think the easiest way (with jQuery) to check if checkbox is checked or NOT is:

    if 'checked':

    if ($(this).is(':checked')) {
    // I'm checked let's do something
    }
    

    if NOT 'checked':

    if (!$(this).is(':checked')) {
    // I'm NOT checked let's do something
    }
    
    0 讨论(0)
  • 2020-12-02 07:54

    Return true if all checbox are checked in a div

    function all_checked (id_div){
     all_checked = true;
    
     $(id_div+' input[type="checkbox"]').each(function() { 
        all_checked = all_checked && $('this').prop('checked');
     }
    
     return all_checked;
    }
    
    0 讨论(0)
  • 2020-12-02 07:57
    if ( $("#checkSurfaceEnvironment-1").is(":checked") && $("#checkSurfaceEnvironment-2").not(":checked") )
    
    0 讨论(0)
提交回复
热议问题