防止单击时取消选中复选框(不禁用或只读) [英] Prevent checkbox from unchecking when clicked (without disable or readonly)

查看:141
本文介绍了防止单击时取消选中复选框(不禁用或只读)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个复选框,我希望人们能够选中/取消选中它。
然而,当他们取消选中它,我使用一个jQueryUI模态弹出来确认他们真的想这样做。因此,他们可以单击确定或取消,我希望我的复选框取消选中,只有当他们单击确定。
这就是为什么我想捕获uncheck事件,以防止复选框在视觉上未检查,如果用户碰巧单击确定,自己程序性地取消选中它。



<



谢谢!



PS:我知道我可以重新检查

pre>

如果用户点击取消,则会触发我不想要的检查事件。 $(#checkboxID)。on(click,function(e){
var checkbox = $(this);
if(checkbox.is ){
//在此执行确认操作
e.preventDefault();
return false;
}
});


I'm using a checkbox and I want people to be able to check/uncheck it. However, when they uncheck it, I'm using a jQueryUI modal popup to confirm that they really want to do that. Thus they can click on OK or Cancel, and I want my checkbox to be unchecked only if they click on OK. That's why I would like to catch the uncheck event to prevent the checkbox from being visually unchecked, and uncheck it myself programaticcaly if the user happens to click on OK.

How could I do that ?

Thanks !

PS: I know I could re-check it after if the user clicks on Cancel but that would trigger the "check" event which I do not want.

解决方案

$("#checkboxID").on("click", function (e) {
    var checkbox = $(this);
    if (checkbox.is(":checked") {
        // do the confirmation thing here
        e.preventDefault();
        return false;
    }
});

这篇关于防止单击时取消选中复选框(不禁用或只读)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆