简体   繁体   中英

keydown on checkbox doesn't bubble in FF, but it does in IE. How to solve?

Using jQuery, I bind keydown globally like this:

$(document).bind('keydown', function(e) { alert('keydown fired'); });

When I press a key having focused a text input field, the alert shows just fine. But when I focus a checkbox and then press a key, it does not fire. Not even with the keypress event. Why do these events not bubble up to the document? They do in Internet Explorer but not in Firefox. How to I fix this? I don't want to bind the keydown explicitly to th checkbox, I want to be able to use delegation. Maybe I can't?

Thx,

/Tommy

Try binding change.

$(document).bind('keydown', function(e) { alert('keydown fired'); })
.bind('change', function(e){ alert('keydown fired'); };

Although it may be better to target check boxes specifically.

$(document).bind('keydown', function(e) { alert('keydown fired'); });
$('input:checkbox').bind('change', function(e){ alert('keydown fired'); };

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