我正在寫一個WWW應用程序,它必須在IE下運行。我在FF下運行的代碼存在問題,但我無法在IE下運行它。輸入類型文本和onKeyDown不工作在IE
// JS代碼
function test()
{
if (window.event.keyCode == 13)
window.location.assign("myPage.php");
}
我已經試過周圍window.location的和location.href,也document.location一些類似的方法。我讀過IE瀏覽器有問題,所以我要求一個解決方案。
目標是,在將某些文字輸入到<input type='text' name='item_code' onKeyDown='test()'>
並點擊輸入後,該頁面會重新加載。所以結果類似於在文本輸入下面按提交類型按鈕。
在IE中重新加載相同的頁面,沒有任何反應。在FF中它正確工作。
UPDATE 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');
線,頁面重新加載剛剛給自己。我不知道這裏有什麼問題?
更新2:
我'粘貼什麼終於爲我工作。
<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.location.href = 「myPage.php」;' – 2010-04-05 10:27:31
'window.location.href'沒有'assign'方法('window.location')。但是,我不認爲這是你的問題,因爲在Firefox中同樣如此(並且會引發錯誤)。你的代碼中還有一些其他的拼寫錯誤(我修正了,但你把它們編輯回來:-)) – 2010-04-05 10:33:51
哦,不知道,請再次編輯它們,如果可以的話,欣賞你的輸入 – qlf00n 2010-04-05 10:35:43