Here is the simplest code that recreates the problem. (In my real world example, I’m resetting the checkbox if there is an error with an ajax call.)
<html>
<script src="http://code.jquery.com/jquery-1.4.2.js"></script>
<script>
$(document).ready( function(){
$('#ckbx').change( function( e ){
alert('fired.');
this.checked = !this.checked;
})
});
</script>
<form>
<input id="ckbx" type="checkbox"></input>
</form>
</html>
In IE9, when I click the checkbox, it displays the alert. Then if I wait any amount of time, then click anywhere else on the page, it fires the event again.
If I remove this.checked = !this.checked the problem goes away.
Can anyone explain this to me? Or provide a way to get around this problem?
UPDATE:
I’m using jquery 1.4.2; in jquery 1.6 this is not a problem. However, we are close to releasing, and we are reluctant to change versions of jquery.
This is because the
.change()event is firing again when you reset the checkbox and click elsewhere on the page. You should use the.click()event instead.