javascript - iCheck plugin - ifToggled cancel event - Stack Overflow

Is it possible to cancel action of iCheck on ifToggled?I have unchecked input:$('input').iCh

Is it possible to cancel action of iCheck on ifToggled? I have unchecked input:

$('input').iCheck().on('ifToggled', function(e) {
    if (confirm('Don\'t check?')) {
        return false;
    }
});

Input still gets checked (JSFiddle simple example). Is there any way to cancel event?

Is it possible to cancel action of iCheck on ifToggled? I have unchecked input:

$('input').iCheck().on('ifToggled', function(e) {
    if (confirm('Don\'t check?')) {
        return false;
    }
});

Input still gets checked (JSFiddle simple example). Is there any way to cancel event?

Share Improve this question edited Sep 7, 2015 at 12:07 Karmalakas asked Sep 7, 2015 at 11:49 KarmalakasKarmalakas 1,1412 gold badges20 silver badges43 bronze badges 5
  • Could you attach a minimal example? JSFiddle – Ramiz Wachtler Commented Sep 7, 2015 at 11:56
  • @RamisWachtler, edited a question. As You can see, it doesn't matter if I confirm or not, input still gets toggled. – Karmalakas Commented Sep 7, 2015 at 12:08
  • So, if you ask 'Dont check?' the initial checkbox state is unchecked ? – Ramiz Wachtler Commented Sep 7, 2015 at 12:33
  • You can change a question to "Don't toggle?" if You like it better :) But yes. – Karmalakas Commented Sep 7, 2015 at 12:39
  • 1 That's a pain in the a** :D All I could find out it this – Ramiz Wachtler Commented Sep 7, 2015 at 12:59
Add a ment  | 

2 Answers 2

Reset to default 4

So I had to stick with not so nice workaround using a timeout

$('input').iCheck({
    checkboxClass: 'icheckbox_flat'
}).on('ifToggled', function(e) {
    if ($('input').is(':checked') && confirm('Don\'t check?')) {
        setTimeout(function() {
           $('input').iCheck('uncheck');
        }, 50);
    }
});

JSFiddle

If anyone has a better solution, would love to hear it.

Throwing an exception seems to cancel the event propagation:

$('input').iCheck({
    checkboxClass: 'icheckbox_flat'
}).on('ifToggled', function(e) {
    if ($('input').is(':checked') && confirm('Don\'t check?')) {
        throw "canceled";
    }
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare./ajax/libs/iCheck/1.0.1/skins/flat/flat.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare./ajax/libs/iCheck/1.0.1/skins/all.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare./ajax/libs/iCheck/1.0.1/icheck.min.js"></script>
<label>Input <input type="checkbox" value="1" name="input"/></label>

PS: I don't like this approach, but it works.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745176675a4615224.html

相关推荐

  • javascript - iCheck plugin - ifToggled cancel event - Stack Overflow

    Is it possible to cancel action of iCheck on ifToggled?I have unchecked input:$('input').iCh

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信