jQuery 如何只允许 shift+enter 在文本区域中返回新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5842813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:53:17  来源:igfitidea点击:

How can I only allow shift+enter to return new line in text area?

jqueryjavascript-eventstextarea

提问by DNB5brims

In a default behavior, the textarea "press" enter will become new line, but I don't want to having a new line, I want the user press "shift+enter", instead. How can I do so? or... ...can I return the textarea enter event before it actually fire to the text area?? Thank you.

在默认行为中,文本区域“按下”回车将成为新行,但我不想换行,我希望用户按下“shift+enter”。我怎么能这样做?或者... ...我可以在 textarea enter 事件真正触发到文本区域之前返回它吗?谢谢你。

回答by BrunoLM

$("textarea").keydown(function(e){
    // Enter was pressed without shift key
    if (e.keyCode == 13 && !e.shiftKey)
    {
        // prevent default behavior
        e.preventDefault();
    }
});

Try the jsFiddle.

试试jsFiddle