Jquery toggle event is messing with checkbox value

前端 未结 10 1055
暗喜
暗喜 2020-11-29 02:01

I\'m using Jquery\'s toggle event to do some stuff when a user clicks a checkbox, like this:

$(\'input#myId\').toggle(
function(){
//do stuff  
},
function()         


        
相关标签:
10条回答
  • 2020-11-29 03:00

    This is an answer by MorningZ (I found it here) that makes totally sense:

    The part you are missing is that "checkbox" is a jQuery object, not a checkbox DOM object

    so:

    checkbox.checked sure would error because there is no .checked property of a jQuery object

    so:

    checkbox[0].checked would work since the first item on a jQuery array is the DOM object itself.

    So in your change() function you can use

    $(this)[0].checked
    
    0 讨论(0)
  • 2020-11-29 03:00

    this worked for me............ check it

    $(":checkbox").click(function(){
    if($(this).attr("id").split("chk_all")[1])
     {
      var ty  = "sel"+$(this).attr("id").split("chk_all")[1]+"[]";
      if($(this).attr("checked"))
       {
        $('input[name="'+ty+'"]').attr("checked", "checked");
       }
      else
       {
        $('input[name="'+ty+'"]').removeAttr("checked");
       }
     }
    })
    
    0 讨论(0)
  • 2020-11-29 03:00
        $("input[type=checkbox][checked=false]")// be shure to set to false on ready
        $("input#Checkbox1").change(function() {
            if ($(this).attr("checked")) {
                $("#chk1").html("you just selected me")//the lable
            } else {$("#chk1").html("you just un selected me") }    
        });
    
    0 讨论(0)
  • 2020-11-29 03:04
    $('input#myId').toggle(
      function(e){
        e.preventDefault();
        //do stuff      
        $(this).attr('checked', 'true');
      },
      function(e){
        e.preventDefault();
        //do other stuff        
        $(this).attr('checked', 'false');
      }
    );
    
    0 讨论(0)
提交回复
热议问题