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
2 Answers
Reset to default 4So 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
评论列表(0条)