简体   繁体   中英

Select Element onChange event is not firing?

I have a select element with an onChange event that does fire when I click the select box and select a new value within it. But, when I tab to the select box and press the up or down arrows to change the select box's value, the event does not fire.

I am using jQuery().change(function(){... }); to set the event

When you tab into a <select> element, the change event doesn't fire until you press the Enter key.

For an onChange() to fire the value must be changed and the input must be blur() -ed (focus moved elsewhere); which is why it fires in your first case, but not in the second.

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

Reference:

As others have stated, the change event doesn't happen until the blur event. You'll need to monitor keyup as well to capture someone moving changing the values with the arrow keys.

Monitor keyup and change,

$('select').bind('change keyup', function() {
   // Handle
});

http://jsfiddle.net/robert/Je26w/

You can trigger the blur event on keyup on the select and then give back focus , which will trigger the change:

$('select').keyup(function(){
  $(this).blur().focus();
});

example: http://jsfiddle.net/niklasvh/XEg36/

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