输入类型文本和onKeyDown不在IE下工作 [英] input type text and onKeyDown not working under IE
问题描述
$ b // JS代码
函数test()
{
if(window.event.keyCode == 13)
window.location.assign myPage.php);
}
我已经尝试了一些类似的方法围绕window.location和location.href,也document.location。我已经读过IE浏览器有问题,所以我要求一个解决方案。
目标是,在将某些文本输入到<$ c $之后重新加载页面c>< input type ='text'name ='item_code'onKeyDown ='test()'> 并点击enter。所以结果类似于在文本输入下按下提交类型按钮。
在IE中重新加载相同的页面,没有任何反应。在FF中它正确的工作。
更新1:
bobince。
< input type ='text'name ='item_code'>
< script type ='text / javascript'>
document.getElementsByName('item_code')[0] .onkeydown = function(event)
{
if(event == undefined){event = window.event; }
if(event.keyCode == 13){window.location ='myPage.php'; }
alert('1');
< / script>;
问题是,如果有 alert('1');
行,页面显示警报和重定向,如果没有 alert('1 ');
line,page只是重新加载到自己。我不知道这里有什么问题?
$ b
更新2:
我正在粘贴最后为我工作的内容。
< input type ='text'name ='item_code'>
< / form>
< script type ='text / javascript'>
document.getElementsByName('item_code')[0] .onkeydown = function(event)
{
if (event == undefined)
{
event = window.event;
}
if(event.keyCode == 13)
{
var js_item_code = document.getElementsByName('item_code')[0] .value;
window.location ='myPage.php?item_code ='+ js_item_code;
return false;
}
};
< / script>
很奇怪,因为您使用 window.event
应确保您的功能仅在IE中可用。这当然是我在尝试你的代码时发生的。我怀疑还有更多你没有向我们展示。
通过内联事件处理程序属性以跨浏览器方式处理事件的通常方法是:
< input type =textname =item_codeonkeydown =test(event)>
function test(event){
if(event.keyCode === 13)
window.location.href ='myPage.php';
$ / code>
这是因为 然而,通常认为避免事件处理程序属性并从JavaScript本身分配处理程序通常会更好。在这种情况下,您需要检查以查看使用哪一个: 一般而言,我只会尝试陷入Enter按键来重现/更改默认表单提交行为作为最后一个采取;如果你可以让表单提交到你想要的地方,那就更好了。 I am writing a WWW application, it has to run under IE. I have the problem with the code that runs under FF, but i can't get it running under IE. // JS code I've tried some similar ways around window.location and location.href, also document.location. I've read that IE has problems with that, so i ask for a solution. The goal is, that page reloads after typing in some text into Within IE it reloads the same page and nothing happens. In FF it correctly works. UPDATE 1: Tried solution given by bobince. The problem is, that if there is UPDATE 2: I'am pasting what finally works for me.
That's strange, because your use of The usual way to handle events in a cross-browser way with inline event handler attributes would be: This works because However, it is generally considered better to avoid event handler attributes and assign handlers from JavaScript itself. In this case you need a check to see which to use: In general I would only try to trap Enter keypresses to reproduce/alter default form submission behaviour as a last resort; better if you can to let the form submit to the place you want it to go. 这篇关于输入类型文本和onKeyDown不在IE下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!事件
在属性中引用IE中的全局 window.event
,其中每个其他浏览器都引用一个名为 event $ c的本地参数$ c>传递给事件处理程序属性函数。
< input type =textname =item_code >
//在输入后的脚本块中,或在文档就绪代码中
//
document.getElementsByName('item_code')[0] .onkeydown = function(事件){
if(event === undefined)event = window.event; //修复IE
if(event.keyCode === 13)
window.location.href ='myPage.php';
};
function test()
{
if (window.event.keyCode == 13)
window.location.assign("myPage.php");
}
<input type='text' name='item_code' onKeyDown='test()'>
and click enter. So the result is similar to pressing submit type button below the text input.<input type='text' name='item_code'>
<script type='text/javascript' >
document.getElementsByName('item_code')[0].onkeydown = function(event)
{
if (event == undefined) { event = window.event; }
if (event.keyCode == 13) { window.location = 'myPage.php'; }
alert('1');
}
</script>";
alert('1');
line, page shows alert and redirects, if there isn't alert('1');
line, page just reloads to itself. I don't know what is the problem here?<form action='mainPage.php' method='POST'>
<input type='text' name='item_code'>
</form>
<script type='text/javascript' >
document.getElementsByName('item_code')[0].onkeydown= function(event)
{
if (event == undefined)
{
event = window.event;
}
if (event.keyCode == 13)
{
var js_item_code = document.getElementsByName('item_code')[0].value;
window.location = 'myPage.php?item_code='+js_item_code;
return false;
}
};
</script>
window.event
should ensure your function only ever works in IE. That's certainly what happens for me when I try your code. I suspect there is more you're not showing us.<input type="text" name="item_code" onkeydown="test(event)">
function test(event) {
if (event.keyCode===13)
window.location.href= 'myPage.php';
}
event
in the attribute refers to the global window.event
in IE, where in every other browser it refers to a local argument named event
passed in to the event handler attribute function.<input type="text" name="item_code">
// in a script block after the input, or in document-ready code
//
document.getElementsByName('item_code')[0].onkeydown= function(event) {
if (event===undefined) event= window.event; // fix IE
if (event.keyCode===13)
window.location.href= 'myPage.php';
};