【问题标题】:How to detect a checkbox click in jQuery如何在jQuery中检测复选框点击
【发布时间】:2015-02-22 13:26:38
【问题描述】:

我无法从下面的脚本中检测到何时以及哪个复选框被点击:

HTML 片段:

<label for="checkbox[1]"><input type="checkbox" name="checkbox[1]" id="checkbox[1]" class="detectThisChange" value="10.00" checked=""> Amount $10.00</label>
<label for="checkbox[2]"><input type="checkbox" name="checkbox[2]" id="checkbox[2]" class="detectThisChange" value="20.00" checked=""> Amount $20.00</label>
<label for="checkbox[3]"><input type="checkbox" name="checkbox[3]" id="checkbox[3]" class="detectThisChange" value="30.00" checked=""> Amount $30.00</label>

jQuery 片段:

$(document).ready(function() {
  $(window).load(function() {
// ... //
        $('.detectThisChange').change(function(event){
          var targetID = triggerEvent.target.id; // get the id that triggered the event
          var posStart = targetID.indexOf('[') + 1;
          var posEnd = targetID.indexOf(']');
          var i = targetID.substring(posStart, posEnd); // get the index of the id that triggered the event

          if ( $('#checkbox\\['+ i +'\\]').prop('checked') != true ) {
            alert('checkbox ' + i + ' was checked');
          }
          else {
            alert('checkbox ' + i + ' was unchecked');
          }

        });
// ... //
  }); // end .load()
}); // end .ready()

附加:

我遇到的问题是我的警报都不起作用。所以这告诉我 change() 函数没有触发。

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/7919716/…
  • ==替换!=
  • 好的,谢谢@97ldave。在选定的答案中,jQuery("#frameName").contents() 的 id frameName 部分代表什么?
  • 谢谢@BhojendraSah。请参阅我的 OQ 中的附加评论。 change() 函数甚至没有触发。
  • 感谢@Invent-Animate。我的问题是我无法使用我的 change() 函数。

标签: javascript jquery checkbox onclick onclicklistener


【解决方案1】:

如果您要动态添加此 HTML,则应使用 .on() 方法,例如:

$(document).on('change', '.detectThisChange', function() {
    // your code
});

试一试,如果有帮助,请告诉我。

【讨论】:

  • 我已经陷入这种情况很多次了,当它不起作用时,这是我检查的第一件事。很高兴有帮助! :)
  • 这是我可以让.on('change', ... 触发的唯一方式。也适用于.on('click',...)。其他人没有。顺便说一句,我也被打了好几个小时,试图用同样的方法让它开火,这当然是失败的。
【解决方案2】:

这样试试

$("input[type=checkbox]").is(":checked") // returns boolean checked or unchecked

   var arr = [];
    $("input[type=checkbox]").each(function () {
        var self = $(this);
        if (self.is(':checked')) {
            arr.push(self.attr("id"));
        }
    });
    console.log(arr);

已编辑:

$("input[type=checkbox]").on('change', function () {
    var self = $(this);
    if (self.is(":checked")) {
        console.log("checkbox  id =" + self.attr("id") + "is checked ");
    } else {
        console.log("Id = " + self.attr("id") + "is Unchecked ");
    }
});

编辑 2:

$("body").on('change','.detectThisChange', function () {
    var self = $(this);
    if (self.is(":checked")) {
        console.log("checkbox  id =" + self.attr("id") + "is checked ");
    } else {
        console.log("Id = " + self.attr("id") + "is Unchecked ");
    }
});

Working Demo

【讨论】:

  • 我需要让我的事件监听器首先工作@Satindersingh。那么我相信你的答案会奏效。
  • 这看起来像是一种新方法。给我一点时间试试@Satindersingh
  • 但是我的脚本@Satindersingh 有超过 20 多个复选框。选中时会触发每个复选框吗?
  • @H.Ferrence :你可以相应地设置你的选择器,如果你想要具有detectThisChange类的元素然后使用$(".detectThisChange").on('change',function(){});
  • 我从一开始就这样做了@Satindersingh。这就是我来这里发帖的原因
【解决方案3】:

尝试change 事件,将此id 选择器设为'input[id^=checkbox]'

$(function() {
  $('input[id^=checkbox]').on('change', function() {
    console.log(this.value, this.checked);
  }).trigger('change');//fire event on page load
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="checkbox[1]">
  <input type="checkbox" name="checkbox[1]" id="checkbox[1]" class="detectThisChange" value="10.00" checked="">Amount $10.00</label>
<label for="checkbox[2]">
  <input type="checkbox" name="checkbox[2]" id="checkbox[2]" class="detectThisChange" value="20.00" checked="">Amount $20.00</label>
<label for="checkbox[3]">
  <input type="checkbox" name="checkbox[3]" id="checkbox[3]" class="detectThisChange" value="30.00" checked="">Amount $30.00</label>

【讨论】:

  • 感谢@Arvind。尽管我知道它对你有用,但它对我不起作用。我只是无法让我的变化着火。
  • @H.Ferrence,控制台有什么错误吗?你用的是什么浏览器?
【解决方案4】:

试试这个:

$(".detectThisChange").on('change', function () {

    alert("In the function");
    var targetID = this.id; // get the id that triggered the event
    var posStart = targetID.indexOf('[') + 1;
    var posEnd = targetID.indexOf(']');
    var i = targetID.substring(posStart, posEnd); // get the index of the id that triggered the event

    if ($('#checkbox\\[' + i + '\\]').prop('checked') == true) {
        alert('checkbox ' + i + ' was checked');
    } else {
        alert('checkbox ' + i + ' was unchecked');
    }
});

这里是 JSFiddle,即使你有答案 :)

【讨论】:

    【解决方案5】:

    试试:)

    if ( $('#checkbox\\['+ i +'\\]').is(":checked") ) {
      alert('checkbox ' + i + ' was checked');
    }
    else {
      alert('checkbox ' + i + ' was unchecked');
    }
    

    【讨论】:

    • 我相信你的答案会在我启动 change() 函数后起作用@BhojendraSah。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多