Internet Explorer showing e.which as undefined

戏子无情 提交于 2019-12-23 03:12:58

问题


In Internet Explorer 8, event.which is showing undefined and working fine in FireFox and IE 9. I am using the Textbox 'onkeypress' Event

MarkUp

<asp:TextBox runat="server" ID="tb1" MaxLength="3"
                        onkeypress="return MainCheckStrings(event);" />

It is working fine in FF, Chrome, Safari and Internet Explorer - 9

Any Idea?


回答1:


var charCode = evt.which || evt.keyCode;



回答2:


The documentation clearly shows that this property is available only from IE 9 onwards.




回答3:


In IE prior 9 the even object is not the same as others so you need to handle that case specifically and same goes for the which property.

function MainCheckStrings(e) {
    if (!e) {
        e = window.event;  // Get event details for IE
        e.which = e.keyCode; // assign which property (so rest of the code works using e.which)
    }
};

Here is a good article on the subject that should explains everything clearly http://www.quirksmode.org/js/keys.html



来源:https://stackoverflow.com/questions/10315260/internet-explorer-showing-e-which-as-undefined

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