標籤:推薦 file content arc 隱藏 斷行符號 pos name idt
1.input斷行符號事件不執行導致頁面重新整理
情境:在文字框中輸入關鍵字按斷行符號,頁面自動重新整理了
<form name="keywordForm" method="post" action=""> <p id="profile_nav"> <label for="profile"> 關鍵字搜尋: </label> <input style="width:80; height:20" type="text" name="keyword" onkeypress="searchKeywordKeyboard(event)" /> <input type="button" value="搜尋" onClick="searchKeyword()"> </p> </form>
解決方案1:
一個表單下,如果只有一個文字框時,按下斷行符號將會觸發表單的提交事件。 既然是只有一個文字框才會出問題,那麼可以加一個隱藏的文字框
解決方案2:(推薦)
<form name="keywordForm" method="post" action="" onsubmit="return false;"> 就是在表單 form 後面加上一個 onsubmit 事件,返回 false,來阻止 form 提交。
解決方案3:(不推薦)
document.onkeydown=function(e){ var e = e || event; var currKey = e.keyCode || e.which || e.charCode;//支援IE,FireFox if (currKey == 13) { return false; } }
解決方案4:
<input type="text" onkeydown="return ClearSubmit(event)" /> function ClearSubmit(e) { if (e.keyCode == 13) { return false; } }
Html input 常見問題