单击时禁止取消选中复选框(不禁用或只读)

我正在使用复选框,我希望人们能够检查/取消选中它。 但是,当他们取消选中它时,我正在使用jQueryUI模式弹出窗口来确认他们确实想要这样做。 因此,他们可以单击“ 确定”或“ 取消” ,我希望仅在单击“ 确定”时取消选中该复选框。
这就是为什么我想要捕获uncheck事件以防止在视觉上取消选中该复选框,并且如果用户碰巧单击确定 ,则以编程方式取消选中它。

我怎么能这样做?

PS:我知道如果用户点击取消 ,我会重新检查它,但这会触发我不想要的check事件。

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

就像是:

 $("#test").on('change', function() { this.checked=!this.checked?!confirm('Really uncheck this one ?'):true; });​ 

小提琴

 $("#yourCheckBoxId").on('change',function(e){ e.preventDefault(); $("#yourPopDiv").popup(); }); 

preventDefault()将禁用input[type="checkbox"]默认行为input[type="checkbox"]

在你的弹出div上

 $("#ok").on('click',function(){ $("#yourCheckBoxId").attr("checked",true); }); 

纯CSS解决方案

选择复选框像 –

 input[type="checkbox"] { pointer-events: none; } 

工作得很好,现在您可以在您选择的任何元素点击上切换复选框。

  $("#checkboxID").click(function(e) { var checkbox = $(this); if (checkbox.is(":checked")) { //check it } else { // prevent from being unchecked this.checked=!this.checked; } });