Event for radio button being selected?
Is there some event for a radio button being selected?
I know I can use "click" but I believe it is possible to style radio buttons in some browsers such that clicking an outter area of a radio button might not select the radio b开发者_JAVA技巧utton.
What event should I use?
It depends when you want to be informed of the event.
If you want to know immediately, go with click
. IE updates the state of checked
before the handler function is called, and I think the other browsers do as well. You may want to double check as I only have IE to work with right now.
If you only need to know before something else happens, you can use change
. IE will not fire the change
event until the selected radio button loses focus. FF/chrome/others may fire the event without focus changing, but I believe IE is actually doing it right in this case.
You can use the onchange
event, which will fire when the radio selection is changed (ie. the first time a radio button in the group is clicked or when the selection within the group is changed).
See http://jsfiddle.net/P9Z9Y/1/ for a simple example (clicking on either the label or the radio button itself will trigger an alert
if the radio group's value changes).
did you try using the onchange event?
Not sure how new the input
event is, but it's generally the best bet. Handles keyboard nav and the like. Updates immediately.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event
精彩评论