Event.observe 'change' events not being triggered in IE

[亡魂溺海] 提交于 2019-12-05 08:26:58

This is a common issue with IE. It does not fire change events until the element loses focus.

To verify that this is indeed the cause of your issue, try changing the menu and then pressing tab to move your focus to another element. If your callback fires properly, you'll know there isn't some other problem.

I have worked around this problem before by also listening for other events like click or keydown. You can add a check to your callback to ensure that the value is actually different from before to ensure that you aren't processing the event more times than necessary (since other browsers will fire both click and change at the same time if they click on a new value).

I found the reason - it wasn't the focus issue, it turns out that I had the form element's name and id values the same - I changed the id value and everything worked fine.

I know this is an old topic but I wanted to share an alternative answer to the one above. In my code the name and id of the element were the same but that didn't matter.

In the function that gets fired the event argument was getting passed in from prototype. To get the value of the text box (that raised the event) in Firefox you use eventArg.currentTarget.value

but in IE you must use eventArg.srcElement.value

So in order for it to work in both browsers you must use...

var value = "";
var id = "";
if(eventArg.currentTarget){//FireFox
value = eventArg.currentTarget.value;
id = eventArg.currentTarget.id;
}
else{//IE
value = eventArg.srcElement.value;
id = eventArg.srcElement.id;
}

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