jQuery 如何使用jQuery处理复选框的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9180087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:28:25  来源:igfitidea点击:

How to handle change of checkbox using jQuery?

jquerycheckbox

提问by BILL

I have some code

我有一些代码

<input type="checkbox" id="chk" value="value" />
<label for="chk">Value </label>
<br/>
<input type="button" id="But1" value="set value" />
<br />
<input type="button" id="But2" value="read checked" />

javascript:

javascript:

$(document).ready(function () {
    console.log("Ready ...");
    registerHandlers();

    function registerHandlers() {
        $('#But1').click(function () {
            $('#chk').prop('checked', !$('#chk').is(':checked'));
        });
        $('#But2').click(function () {
            var chk1 = $('#chk').is(':checked');
            console.log("Value : " + chk1);
        });

        $('input[type="checkbox"]').change(function () {
            var name = $(this).val();
            var check = $(this).prop('checked');
            console.log("Change: " + name + " to " + check);
        });
    }
});

How to handle change of checkbox using jQuery ? I need to put the handler to change any checkboxes checked.

如何使用 jQuery 处理复选框的更改?我需要让处理程序更改选中的任何复选框。

[update]

[更新]

There is a checkbox and a few buttons. Each button can change check box. How to catch an event changing the checkbox?

有一个复选框和几个按钮。每个按钮都可以更改复选框。如何捕获更改复选框的事件?

[Update]

[更新]

I need handle change checkbox in this example jsfiddle. When I click on the box the message "OK" button is not shown.

在此示例jsfiddle 中,我需要处理更改复选框。当我单击该框时,未显示消息“确定”按钮。

回答by Samich

Use :checkboxselector:

使用:checkbox选择器:

$(':checkbox').change(function() {

        // do stuff here. It will fire on any checkbox change

}); 

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

代码:http: //jsfiddle.net/s6fe9/

回答by Pinkesh Sharma

You can use Id of the field as well

您也可以使用字段的 ID

$('#checkbox1').change(function() {
   if($(this).is(":checked")) {
      //'checked' event code
      return;
   }
   //'unchecked' event code
});

回答by Lokesh Yadav

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'
});

回答by Arjun

$("input[type=checkbox]").on("change", function() { 

    if (this.checked) {

      //do your stuff

     }
});

回答by Everton Z. P.

$('#myForm').on('change', 'input[type=checkbox]', function() {
    this.checked ? this.value = 'apple' : this.value = 'pineapple';
});

回答by laurent belloeil

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

在我看来 removeProp 在 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);
    };

回答by Siva Anand

get radio value by name

名称获取无线电值

  $('input').on('className', function(event){
        console.log($(this).attr('name'));
        if($(this).attr('name') == "worker")
            {
                resetAll();                 
            }
    });