简体   繁体   English

中断“输入”键与表单提交的关联

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

I want to detect 'enter' key with respect to form without letting the form being submitted. 我想检测有关表单的“输入”键,而不要提交表单。 How do I break this association? 我如何打破这种联系?

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

} }

Though actions inside are taken properly, the page reloads upon completion. 尽管内部操作已正确执行,但页面在完成后会重新加载。

Try to return false at the end of your function. 尝试在函数末尾return false Assuming that you're handling the form submission already in a submit event callback. 假设您已经在submit事件回调中处理了表单提交。

Return false from your function: 从函数返回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