JavaScript KeyCode值是“未定义的”在Internet Explorer 8中 [英] JavaScript KeyCode Values are "undefined" in Internet Explorer 8
问题描述
我遇到了一些我编写过的JavaScript问题,但只能使用Internet Explorer 8.我在Internet Explorer 7或更早版本或Mozilla Firefox 3.5或更早版本上执行此操作时没有问题。当我在Internet Explorer 8上使用兼容模式时,它也可以正常执行。
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.
当用户在文本框中输入值时,我正在做的是覆盖Enter键击。所以在我的元素上我有这个:
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>
然后我有以下JavaScript方法:
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();
}
同样,这一切都适用于几乎所有其他浏览器。 Internet Explorer 8只是让我很难过。
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!
更新:感谢大家的快速反馈。 Chris Pebble和Bryan Kyle都协助解决了这个问题。我已经授予布莱恩答案以帮助他的声誉。谢谢大家!
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!
推荐答案
在IE8下看起来像 keyCode
属性 window.Event
是 undefined
但 window.event的相同属性
(注意小写 e )具有值。您可以尝试使用 window.event
。
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();
}
这篇关于JavaScript KeyCode值是“未定义的”在Internet Explorer 8中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!