是否可以取消iCheck在ifToggled上的操作?我有未经检查的输入:
$('input').iCheck().on('ifToggled', function(e) {
if (confirm('Don\'t check?')) {
return false;
}
});仍然检查输入(JSFiddle简单示例)。有什么办法取消活动吗?
发布于 2022-06-23 20:03:10
抛出异常似乎会取消事件传播:
$('input').iCheck({
checkboxClass: 'icheckbox_flat'
}).on('ifToggled', function(e) {
if ($('input').is(':checked') && confirm('Don\'t check?')) {
throw "canceled";
}
});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.1/skins/flat/flat.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.1/skins/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.1/icheck.min.js"></script>
<label>Input <input type="checkbox" value="1" name="input"/></label>
PS:我不喜欢这种方法,但它有效。
发布于 2015-09-11 07:33:19
所以我不得不坚持用超时来解决这个问题。
$('input').iCheck({
checkboxClass: 'icheckbox_flat'
}).on('ifToggled', function(e) {
if ($('input').is(':checked') && confirm('Don\'t check?')) {
setTimeout(function() {
$('input').iCheck('uncheck');
}, 50);
}
});JSFiddle
如果有人有更好的解决方案,我很想听听。
https://stackoverflow.com/questions/32438001
复制相似问题