jQuery复选框更改并单击事件

Pro*_*aos 528 javascript jquery event-handling

我有

$(document).ready(function() {
  //set initial state.
  $('#textbox1').val($(this).is(':checked'));

  $('#checkbox1').change(function() {
    $('#textbox1').val($(this).is(':checked'));
  });

  $('#checkbox1').click(function() {
    if (!$(this).is(':checked')) {
      return confirm("Are you sure?");
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="checkbox1"/><br />
<input type="text" id="textbox1"/>
Run Code Online (Sandbox Code Playgroud)

JSFIDDLE链接

此处更改的事件使用复选框状态更新文本框值.我使用click事件确认取消选中时的操作.如果用户选择取消,则复选标记将恢复,但更改甚至会在确认之前触发,使事物处于不一致状态(选中复选框时文本框显示为false).如何处理取消并保持文本框值与检查状态一致?

kas*_*ega 844

好吧,我没有看到与我匹配的答案,所以我会发布这个.在JSFiddle中测试并执行您要求的操作.当单击与复选框关联的标签时,此方法具有触发的额外好处.

原答案:

$(document).ready(function() {
    //set initial state.
    $('#textbox1').val(this.checked);

    $('#checkbox1').change(function() {
        if(this.checked) {
            var returnVal = confirm("Are you sure?");
            $(this).prop("checked", returnVal);
        }
        $('#textbox1').val(this.checked);        
    });
});
Run Code Online (Sandbox Code Playgroud)

更新答案:

$(document).ready(function() {
    //set initial state.
    $('#textbox1').val($(this).is(':checked'));

    $('#checkbox1').change(function() {
        if($(this).is(":checked")) {
            var returnVal = confirm("Are you sure?");
            $(this).attr("checked", returnVal);
        }
        $('#textbox1').val($(this).is(':checked'));        
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 只需注意,使用`this.checked`代替`$(this).is(':checked')`的速度要快得多:http://jsperf.com/prop-vs-ischecked/5 (120认同)
  • @MikeU:就过早优化问题与你达成一致,但考虑到`this.checked`比编写+原生javascript要短得多,一般来说它可能真的值得使用(除了快200到300倍). (50认同)
  • @Dakota当然,它要慢得多,但我们仍在谈论600k操作/秒.所以,每毫秒600次.我认为如果这会导致您的网页出现性能问题,您可能需要重新评估您的javascript;)不过,最好通过代码了解性能指标.谢谢. (36认同)
  • 我建议使用.prop()而不是.attr(),因为后者在所有情况下都不起作用,我相信jQuery现在推荐使用.prop(). (10认同)
  • 我认为$('#textbox1')。val(this.checked);中的[this]; 指文件。应该是$('#textbox1')。val($('#checkbox1')。is(':checked')); (2认同)

Jos*_*kle 88

演示

使用 mousedown

$('#checkbox1').mousedown(function() {
    if (!$(this).is(':checked')) {
        this.checked = confirm("Are you sure?");
        $(this).trigger("change");
    }
});
Run Code Online (Sandbox Code Playgroud)

  • @frinux这很简单,因为_it是一个鼠标相关的问题.请不要根据它们与完全分开的问题的相关性来回答.如果您在通过键盘选中复选框后触发指示灯状态时遇到问题,请发布有关它的问题,而不是粗心地低估回答. (3认同)
  • 因为mousedown而不是用户可以与复选框进行交互的唯一方式. (3认同)

lko*_*lko 47

如果你使用的话,大多数答案都不会捕捉它(大概)<label for="cbId">cb name</label>.这意味着当您单击标签时,它将选中该框而不是直接单击该复选框.(不完全是问题,但各种搜索结果往往会出现在这里)

<div id="OuterDivOrBody">
    <input type="checkbox" id="checkbox1" />
    <label for="checkbox1">Checkbox label</label>
    <br />
    <br />
    The confirm result:
    <input type="text" id="textbox1" />
</div>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以使用:

Earlier versions of jQuery:

$('#OuterDivOrBody').delegate('#checkbox1', 'change', function () {
    // From the other examples
    if (!this.checked) {
        var sure = confirm("Are you sure?");
        this.checked = !sure;
        $('#textbox1').val(sure.toString());
    }
});
Run Code Online (Sandbox Code Playgroud)

使用jQuery 1.6.4的JSFiddle示例

jQuery 1.7+

$('#checkbox1').on('change', function() { 
    // From the other examples
    if (!this.checked) {
        var sure = confirm("Are you sure?");
        this.checked = !sure;
        $('#textbox1').val(sure.toString());
    }
});
Run Code Online (Sandbox Code Playgroud)

使用最新的jQuery 2.x的JSFiddle示例

  • 添加了jsfiddle示例和带有可单击复选框标签的html


zar*_*run 24

嗯..只是为了挽救头痛(过去的午夜),我可以想出:

$('#checkbox1').click(function() {
  if (!$(this).is(':checked')) {
    var ans = confirm("Are you sure?");
     $('#textbox1').val(ans);
  }
});
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你


chr*_*ris 10

对我来说这很有用:

$('#checkboxID').click(function () {
    if ($(this).attr('checked')) {
        alert('is checked');
    } else {
        alert('is not checked');
    }
})
Run Code Online (Sandbox Code Playgroud)

  • 通过网络搜索遇到了这个.您应该使用'prop'而不是'attr'>> http://api.jquery.com/prop/ (4认同)

Dev*_*per 10

这个给你

HTML

<input id="ProductId_a183060c-1030-4037-ae57-0015be92da0e" type="checkbox" value="true">
Run Code Online (Sandbox Code Playgroud)

JavaScript的

<script>
    $(document).ready(function () {

      $('input[id^="ProductId_"]').click(function () {

        if ($(this).prop('checked')) {
           // do what you need here     
           alert("Checked");
        }
        else {
           // do what you need here         
           alert("Unchecked");
        }
      });

  });
</script>
Run Code Online (Sandbox Code Playgroud)


Ped*_*ito 10

迟到的答案,但您也可以使用 on("change")

$('#check').on('change', function() {
     var checked = this.checked
    $('span').html(checked.toString())
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="check"> <span>Check me!</span>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,上面的答案已经很详细了,你的很简单也足够了。谢谢 (2认同)

小智 7

只需使用单击事件我的复选框 id 是 CheckAll

     $('#CheckAll').click(function () {

        if ($('#CheckAll').is(':checked') == true) {

             alert(";)");
      }
    });
Run Code Online (Sandbox Code Playgroud)


yoo*_*er8 6

摆脱更改事件,而是更改单击事件中文本框的值.而不是返回确认的结果,在var中捕获它.如果是真的,请更改该值.然后返回var.


Mrc*_*ief 6

复选框单击并检查同一事件循环中的值是问题.

试试这个:

$('#checkbox1').click(function() {
    var self = this;
    setTimeout(function() {

        if (!self.checked) {
            var ans = confirm("Are you sure?");
            self.checked = ans;
            $('#textbox1').val(ans.toString());
        }
    }, 0);
});
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/mrchief/JsUWv/6/


ume*_*mer 6

如果您使用的是iCheck Jquery,请使用以下代码

 $("#CheckBoxId").on('ifChanged', function () {
                alert($(this).val());
            });
Run Code Online (Sandbox Code Playgroud)


Sha*_*oli 5

试试这个

$('#checkbox1').click(function() {
        if (!this.checked) {
            var sure = confirm("Are you sure?");
            this.checked = sure;
            $('#textbox1').val(sure.toString());
        }
    });
Run Code Online (Sandbox Code Playgroud)


Jas*_*son 5

$(document).ready(function() {
    //set initial state.
    $('#textbox1').val($(this).is(':checked'));

    $('#checkbox1').change(function() {
        $('#textbox1').val($(this).is(':checked'));
    });

    $('#checkbox1').click(function() {
        if (!$(this).is(':checked')) {
            if(!confirm("Are you sure?"))
            {
                $("#checkbox1").prop("checked", true);
                $('#textbox1').val($(this).is(':checked'));
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


Mar*_*Zen 5

// this works on all browsers.

$(document).ready(function() {
    //set initial state.
    $('#textbox1').val($(this).is(':checked'));

    $('#checkbox1').change(function(e) {
        this.checked =  $(this).is(":checked") && !!confirm("Are you sure?");
        $('#textbox1').val(this.checked);
        return true;
    });
});
Run Code Online (Sandbox Code Playgroud)