我想捕捉回車鍵使按鍵點擊捕獲回車鍵使按鍵點擊使用JavaScript
我有此javascript:
function doClick(buttonName,e)
{
//the purpose of this function is to allow the enter key to
//point to the correct button to click.
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
//Get the button the user wants to have clicked
var btn = document.getElementById('submit');
if (btn != null)
{ //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
與HTML
<input type="button" id="submit" value="Search" onClick="doSomeThing();" />
<input type="text" name="search" onKeyPress="doClick('submit',event)" />
這是工作正常的IE瀏覽器,但它沒有與Firefox,
爲什麼?任何人都可以修復這個JavaScript代碼在所有瀏覽器上工作。
謝謝
你能解釋發生了什麼?該方法是否被調用? – cwallenpoole 2010-12-05 12:11:42
是的。謝謝 – Swell 2010-12-05 12:48:59
此鏈接將幫助你明確 http://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box – 2012-08-16 07:02:35