Lose Focus from Select Box with Jquery .change() event

送分小仙女□ 提交于 2020-01-05 04:12:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!