4

just like the example: http://jsfiddle.net/nicaia/6cHxR/

the js code:

$('#checkbox_id').bind('change',function({
    alert('change');
}).bind('click',function(event){
    alert('click');event.preventDefault();
});

in chrome click the checkbox will show this:

alert 'change' and alert 'click' and the checkbox will not be checked.(the checkbox is uncheck in first.)

and in firefox click the checkbox will show this: alert 'click' and the checkbox will not be checked.(the checkbox is uncheck in first.)

the change will not be triggered in firefox. i don't know why.somebody can tell me?

thanks.

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
Bodil
  • 151
  • 1
  • 10
  • 3
    I think it is because the checkbox loses focus after alert. Therefore 'change' event is not triggered http://api.jquery.com/change/ – Sergey Ratnikov Dec 23 '11 at 10:31
  • but in chrome the change event will be triggered. – Bodil Dec 23 '11 at 11:05
  • [link](http://stackoverflow.com/questions/7031226/jquery-checkbox-change-and-click-event)this question and my question is a bit like. and thanks @Nicola – Bodil Dec 28 '11 at 08:24
  • @Sergey You will find the same issue if you replace alert with console.log. – Alex Jan 22 '13 at 06:56

1 Answers1

0

I think that chrome has a different behaviour from other browsers. I tried this code:

$('#checkbox_id').bind('change',function(){
    alert('change');
}).bind('click',function(event){
    alert('click');
});

(fiddle here: http://jsfiddle.net/6cHxR/8/) and Firefox execute the click handler before the change handler while IE and chrome execute the change handler before the click handler. I don't think that you can do a lot about that

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • in the click function this code "event.preventDefault()" stop the change event execute in firefox,because the click handler is executed before the change handler.why the click handler will be executed before change handler in firefox? – Bodil Dec 23 '11 at 11:17
  • @Bodil i think that firefox handles event differently from other browsers. I don't know if there is a specification for this and so every vendor makes his implementetion. to me it makes more sense that the click handler execute before the change handler, but that's my opinion – Nicola Peluchetti Dec 23 '11 at 13:22