for iCheck plugin, is there a way to avoid "ifChanged" event handler to fire up when setting the checkbox from Javascript?

有帮助吗?

解决方案 2

You could create a variable ignoreChange and when subscribing to the event handler, checking whether that variable is true and if it is, then set it to false and stop the function. If it is not true, then you can execute your normal code.

JS code:

var ignoreChange = false;

$('input').on('ifChanged', function(event){

    if (ignoreChange) {
        ignoreChange = false;
        return;
    }

    // do stuff
});

// When changing the checkbox
ignoreChange = true;

Basically, whenever you set the variable ignoreChange to true, the next event call is ignored. This is quite a hacky workaround, however necessary, as I did not find a way to solve your problem trough the iCheck library.

其他提示

Old question, but I found a better method is to check the event.target.checked property and only run your code if it returns true. iCheck fires ifChanged twice - first for the un-checked option, then secondly for the checked option. So if you only run your code if event.target.checked === true, you will get the result you are after.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top