How to handle change of checkbox using jQuery?

后端 未结 7 1832
失恋的感觉
失恋的感觉 2020-12-02 15:02

I have some code




相关标签:
7条回答
  • 2020-12-02 15:26
    $('#myForm').on('change', 'input[type=checkbox]', function() {
        this.checked ? this.value = 'apple' : this.value = 'pineapple';
    });
    
    0 讨论(0)
  • 2020-12-02 15:30

    It seems to me removeProp is not working properly in Chrome : jsfiddle

            $('#badBut1').click(function () {
            checkit('Before');
            if( $('#chk').prop('checked') )
            {
              $('#chk').removeProp('checked');
            }else{
                $('#chk').prop('checked', true);
            }
            checkit('After');
        });
        $('#But1').click(function () {
            checkit('Before');
            if( $('#chk').prop('checked') )
            {
              $('#chk').removeClass('checked').prop('checked',false);
            }else{
                $('#chk').addClass('checked').prop('checked', true);
            }
            checkit('After');
        });
    
        $('#But2').click(function () {
            var chk1 = $('#chk').is(':checked');
            console.log("Value : " + chk1);
        });
    
        $('#chk').on( 'change',function () {
            checkit('Result');
        });
        function checkit(moment) {
            var chk1 = $('#chk').is(':checked');
            console.log(moment+", value = " + chk1);
        };
    
    0 讨论(0)
  • 2020-12-02 15:31

    Use :checkbox selector:

    $(':checkbox').change(function() {
    
            // do stuff here. It will fire on any checkbox change
    
    }); 
    

    Code: http://jsfiddle.net/s6fe9/

    0 讨论(0)
  • 2020-12-02 15:35

    Hope, this would be of some help.

    $('input[type=checkbox]').change(function () {
        if ($(this).prop("checked")) {
            //do the stuff that you would do when 'checked'
    
            return;
        }
        //Here do the stuff you want to do when 'unchecked'
    });
    
    0 讨论(0)
  • 2020-12-02 15:38

    get radio value by name

      $('input').on('className', function(event){
            console.log($(this).attr('name'));
            if($(this).attr('name') == "worker")
                {
                    resetAll();                 
                }
        });
    
    0 讨论(0)
  • 2020-12-02 15:40

    You can use Id of the field as well

    $('#checkbox1').change(function() {
       if($(this).is(":checked")) {
          //'checked' event code
          return;
       }
       //'unchecked' event code
    });
    
    0 讨论(0)
提交回复
热议问题