Check/Uncheck - ifChecked 不工作

Check/Uncheck - ifChecked not working

更新:我正在使用以下 plugin

我正在尝试检查当用户刷新页面或重新加载页面时复选框是否被选中,这是我使用的但我已经完成调试并且它从未执行 second IF 条件

$('input').on('ifChecked', function (event) {
    if ($("input#CreatePart").is(':checked')) {
        alert('checked');
    }
});

$('input').on('ifUnchecked', function (event) {
    if ($("input#CreatePart").is(':checked')) {
        alert('un-checked');
    }
});

<input checked="checked" class="icheck" data-val="true" 
  data-val-required="The Create new Part field is required." 
  id="CreatePart" name="CreatePart" type="checkbox" value="true" 
/>
<input name="CreatePart" type="hidden" value="false" />

change 事件上使用,而不是在使用 ifChecked 或 ifUnchecked

时使用
$('input.icheck').on('change', function (event) {
    if ($("input#CreatePart").is(':checked')) {
        alert('checked');
    }else{
       alert("unchecked");
    }

});

尝试:

页面加载时:

 alert($('input').is(':checked'));

http://jsfiddle.net/9ypwjvt4/18/

否则使用:

   $(document).ready(function () {
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-orange',
        radioClass: 'iradio_square-orange',
        increaseArea: '20%' // optional
    });

    $('input').on('ifChecked', function(event){
      alert('Checked');
    });

    $('input').on('ifUnchecked', function(event){
      alert('unchecked');
    });
});

jsfiddle: http://jsfiddle.net/9ypwjvt4/17/

更改 ifUnchecked 条件以确保复选框未选中而不是选中。

当未选中的事件被触发时,您正在检查该框是否被选中,而这永远不会发生。

$('input').on('ifUnchecked', function (event) {
    if ($("input#CreatePart").not(':checked')) {
        alert('un-checked');
    }
});

演示:

http://jsfiddle.net/ejLbgt7b/

在我的例子中,'ifChecked' 事件也没有触发,但后来我注意到我在代码的不同位置初始化 iCheck 两次。

$('input').iCheck();
// and later in the code:
$('input').iCheck();

删除冗余初始化有帮助,'ifChecked' 开始工作。