簡體   English   中英

中斷“輸入”鍵與表單提交的關聯

[英]break association of 'enter' key with form submission

我想檢測有關表單的“輸入”鍵,而不要提交表單。 我如何打破這種聯系?

document.forms[0].onkeypress = function (event) {
    e = window.event ? window.event : event;
    if (e.keyCode == 13) {
        //actions to be taken
    }    

}

盡管內部操作已正確執行,但頁面在完成后會重新加載。

嘗試在函數末尾return false 假設您已經在submit事件回調中處理了表單提交。

從函數返回false

document.forms[0].onkeypress = function (event) {
    e = window.event ? window.event : event;
    if (e.keyCode == 13) {
        //actions to be taken 

        return false;
    }    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM