iCheck 撤消检查

iCheck undo check

我想在选中复选框时进行一些验证,如果验证失败则取消选中它。

现在,选中的 属性 为 false,但复选框仍处于选中状态。

我的代码:

$("#Lve_InclWE").on('ifChecked', function(event){
    some_function();
    if(!some_validation())
        $('#Lve_InclWE').iCheck('uncheck');
});

HTML:

<div class="icheckbox_flat-green checked" style="position: relative;">
<input type="checkbox" class="icheck" id="Lve_InclWE" name="Lve_InclWE" style="position: absolute; opacity: 0;">

update1:​​即使我这样做了,问题依然存在:

$("#Lve_InclWE").removeProp("checked"); 
//$("#Lve_InclWE").prop("checked",false);
$("#Lve_InclWE").iCheck('update');

update2:尝试删除在父级 div 处选中的 class,但还是一样。是因为我在检查事件中执行了吗?

$('#Lve_InclWE').closest("div").removeClass("checked");

update3: 我在 "ifChecked" 事件结束时添加了一个警报,并注意到该复选框实际上未被选中,但它 returns 被选中活动结束后的状态。也就是说我无法在触发的事件中执行undo check(uncheck),你能想到什么方法吗?

解决方案和可能的唯一解决方案是在事件结束后取消选中复选框,使用 setTimeOut(fn,0)

$("#Lve_InclWE").on('ifChecked', function(event){
    if(!validation()){
        setTimeout(function(){
            $('#Lve_InclWE').iCheck('uncheck');
        },0);
    }
});

原因是因为checkbox总是在触发事件后设置为checked状态。

如果您有其他解决方案,请随时发表评论。

一种可能的解决方案是销毁并重新创建 iCheck。

$("#Lve_InclWE").on('ifChecked', function(event){
    some_function();
    if(!some_validation()){  
        $('#Lve_InclWE').iCheck('destroy');
        $('#Lve_InclWE').prop('checked', true);
        /*
            Now reinitialize iCheck for #Lve_InclWE
        */
    }       
});