如何防止返回/输入除文本区域以外的所有表单元素?

时间:2022-08-17 15:54:42

I recently took over an existing project and ran into problems with textareas and carriage return not working. I found the code below after some digging:

我最近接手了一个现有的项目,并遇到了文本区域和回车不工作的问题。我在挖掘后发现下面的代码:

$(window).keydown(function (event) {
    if (event.keyCode == 13) {
        event.preventDefault();
        return false;
    }
});

So, I take it this is to prevent form elements from submitting the form when a visitor/user press carriage return.

因此,我认为这是为了防止表单元素在访问者/用户按回车时提交表单。

Any idea how I can keep the functionality of the above code-snippet, and still allow carriage returns in textareas?

知道如何保持上面代码段的功能,并允许在textareas中返回回车吗?

1 个解决方案

#1


2  

just to close this question, I found a solution for the followup-question: How to prevent return/enter on all form elements except textareas.

为了解决这个问题,我找到了一个后续问题的解决方案:如何防止在textareas之外的所有表单元素上返回/输入。

$(document).keypress(function (e) {
    if (e.which == 13 && e.target.nodeName != "TEXTAREA") {
        event.preventDefault();
        return false;
    }
});

#1


2  

just to close this question, I found a solution for the followup-question: How to prevent return/enter on all form elements except textareas.

为了解决这个问题,我找到了一个后续问题的解决方案:如何防止在textareas之外的所有表单元素上返回/输入。

$(document).keypress(function (e) {
    if (e.which == 13 && e.target.nodeName != "TEXTAREA") {
        event.preventDefault();
        return false;
    }
});