为了账号安全,请及时绑定邮箱和手机立即绑定

jQuery复选框更改并单击事件

jQuery复选框更改并单击事件

有只小跳蛙 2019-07-25 18:53:05
jQuery复选框更改并单击事件$(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?");     }   });});<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"/>
查看完整描述

3 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

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

原答案:

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

更新答案:

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


查看完整回答
反对 回复 2019-07-25
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

演示

使用 mousedown

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


查看完整回答
反对 回复 2019-07-25
  • 3 回答
  • 0 关注
  • 632 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信