简体   繁体   中英

Checkbox click event fires before change event in Firefox unlike in Chrome

I have two events for a check-box in my script one on click and another on change. When I click on it it fires change event first then click event in Chrome but in Firefox it's reverse. Is there any way to change the order of it in Firefox?

I don't know why the order in which the events are fired is inconsistent between browsers, but I don't see why you can't move everything in to a single change handler. If you must use separate handlers, you could add a timeout, the click event would still fire first in Firefox, but its code wouldn't be executed until after the change handler fires.

var myBoolean = false;

$('#example').on({
    'click': function() {
        setTimeout(function() {
            console.log('clicked'); 
            console.log(myBoolean);
        }, 1);
    },
    'change': function() {
        console.log('changed');
        myBoolean = true;
    }
});

Here's a fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM