JavaScript KeyCode Values are “undefined” in Inter

2020-01-27 07:17发布

I'm having trouble with some JavaScript that I've written, but only with Internet Explorer 8. I have no problem executing this on Internet Explorer 7 or earlier or on Mozilla Firefox 3.5 or earlier. It also executes properly when I use compatibility mode on Internet Explorer 8.

What I'm doing is overriding the Enter keystroke when a user enters a value into a textbox. So on my element I have this:

<asp:TextBox ID="ddPassword" runat="server" TextMode="Password" onkeypress="doSubmit(event)" Width="325"></asp:TextBox>

And then I have the following JavaScript method:

function doSubmit(e)
{
    var keyCode = (window.Event) ? e.which : e.keyCode;
    if (keyCode == 13)
        document.getElementById("ctl00_ContentPlaceHolder1_Login").click();  
}

Again, this all works fine with almost every other browser. Internet Explorer 8 is just giving me a hard time.

Any help you might have is greatly appreciated. Thanks!

UPDATE: Thanks everyone for your quick feedback. Both Chris Pebble and Bryan Kyle assisted with this solution. I have awarded Bryan the "answer" to help with his reputation. Thanks everyone!

8条回答
We Are One
2楼-- · 2020-01-27 07:55

It looks like under IE8 the keyCode property of window.Event is undefined but that same property of window.event (note the lowercase e) has the value. You might try using window.event.

function doSubmit(e)
{
   var keyCode = (window.event) ? e.which : e.keyCode;
   if (keyCode == 13)
      document.getElementById("ctl00_ContentPlaceHolder1_Login").click();  
}
查看更多
唯我独甜
3楼-- · 2020-01-27 07:55

It's worked on this way on my code:

var kcode = (window.event) ? event.keyCode : event.which;
查看更多
登录 后发表回答