【问题标题】:How to handle change of checkbox using jQuery?如何使用 jQuery 处理复选框的更改?
【发布时间】:2012-02-29 02:34:12
【问题描述】:

我有一些代码

<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:

$(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);
        });
    }
});

如何使用 jQuery 处理复选框的更改? 我需要将处理程序更改为选中的所有复选框。

[更新]

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

[更新]

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

【问题讨论】:

  • 我不太明白你的问题?更改复选框时应该发生什么?
  • 有什么问题?这段代码似乎工作得很好
  • 能否请您重新表述您的问题?你已经有了 $('input[type="checkbox"]').change 处理程序,怎么了?
  • 如果更改按钮复选框更改事件不会发生
  • 仅供参考; $('#chk').prop('checked') 返回一个布尔值而不是属性的值。见api.jquery.com/prop

标签: jquery checkbox


【解决方案1】:

使用:checkbox 选择器:

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

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

}); 

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

【讨论】:

  • ...和this.checked 对于确定复选框是否被选中非常有用。
  • 是否可以注册多个处理程序?
  • 2015年还推荐这个吗?
  • 好像API没变,所以是的。
  • 这需要在 $(document).ready(function ()... 对我来说,对吗?
【解决方案2】:

希望,这会有所帮助。

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

【讨论】:

  • 从 jQuery 1.6 开始,最好使用 $(this).prop("checked"),因为它会随着复选框的实际状态而动态变化。
  • .attr("checked") 不正确,因为它不会随着用户点击而更新。我确认@hrabinowitz 的评论。
【解决方案3】:

你也可以使用字段的ID

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

【讨论】:

    【解决方案4】:
    $('#myForm').on('change', 'input[type=checkbox]', function() {
        this.checked ? this.value = 'apple' : this.value = 'pineapple';
    });
    

    【讨论】:

    • 请详细说明您的答案:纯代码解决方案更有可能被删除,因为它们不解释,只需修复(如果有的话)。
    • 对不起@rekaszeru 我下次会做得更好
    【解决方案5】:
    $("input[type=checkbox]").on("change", function() { 
    
        if (this.checked) {
    
          //do your stuff
    
         }
    });
    

    【讨论】:

    • 请在发布前检查您自己的代码作品。您的选择器存在明显问题...
    • live() 已弃用。
    【解决方案6】:

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

    【讨论】:

    【解决方案7】:

    通过名称获取电台值

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

    【讨论】:

      猜你喜欢
      • 2020-02-05
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2012-02-26
      • 2017-11-22
      • 1970-01-01
      相关资源
      最近更新 更多