【问题标题】:How to show alert/confirmation only on uncheck of check box如何仅在取消选中复选框时显示警报/确认
【发布时间】:2016-09-30 21:30:25
【问题描述】:

我有一个asp:CheckBoxList,我想仅在使用 jquery 取消选中复选框时显示警告消息。

   $('.chklist').click(
     function () {
         if ($(this).is(":not(:checked)")) {
             alert(this.id);
         }

     });

但上面的代码总是给出true,即使项目被选中或未选中。因为它为整个列表而不是那个特定的复选框使用 id。我不想在回发事件中实现这些东西。

【问题讨论】:

    标签: javascript jquery asp.net checkbox


    【解决方案1】:

    我做了一些用户取消选中复选框的示例。 希望这就是你要找的。祝你好运!

    $('#check').on('click', function(){
    if(!$(this).is(':checked')) {
        // do something if the checkbox is NOT checked
        alert(this.id);
    }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    Check<input type="checkbox" id="check"/>

    【讨论】:

      【解决方案2】:

      您应该在 CheckBoxList 的输入元素上设置 click 事件处理程序,而不是列表本身。假设 CheckBoxList 有CssClass="chklist"

      $('.chklist input').click(function () {
          if (!$(this).is(':checked')) {
              alert(this.id);
          }
      });
      

      它也适用于您的原始代码:

      $('.chklist input').click(function () {
          if ($(this).is(":not(:checked)")) {
              alert(this.id);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-01
        • 2019-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-08
        相关资源
        最近更新 更多