问题
I am using a dropdown which displays several form fields after you choose something from the dropdown. I have bound the dropdown with jquery to detect a change, however, the box must 'lose focus' before the .change is triggered. Any way to fix this?
$("#EmployeeSelected").change(function () {
alert('hi');
});
回答1:
It doesn't appear to require losing focus, at least with an traditional change. If you are using another form of changing, ie keystrokes, you may want to consider binding additional events to it.
It should fire as you have it, as long as you are selecting it in the traditional sense (Clicking the option). If you are using a different method, such as keyboard input, you will need to bind the proper events to it, such as keypress
, keydown
, etc.
Working Demo
回答2:
If you want to trigger a change event when using the keyboard up/down keys in a dropdown list you could hook up with the keypress
and keydown
events. There is a bug in IE which triggers the change
event when cycling through the options of a dropdown with the arrows but this is not the default behavior and it is dangerous. This behavior is not the same for other browsers when the change event is not triggered with the up/down keys.
来源:https://stackoverflow.com/questions/7288565/lose-focus-from-select-box-with-jquery-change-event