admin管理员组

文章数量:1415652

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.

本文标签: javascriptiCheck pluginifToggled cancel eventStack Overflow