简体   繁体   English

如何取消焦点文本区域中的换行符

[英]How to disbale line breaks in textarea on focus

So I have a textbox(input) and I have added a functionality to it that if the user presses enter, the focus shifts to a textarea element.所以我有一个文本框(输入),我已经添加了一个功能,如果用户按下回车键,焦点就会转移到文本区域元素。 But for some reason, a line break gets added after focus on the textarea and the cursor appears.但由于某种原因,在关注文本区域并出现 cursor 后添加了一个换行符。 I tried using (keydown.enter)="$event.preventDefault()" on the textarea but it only prevents line breaks when the cursor is in the textarea.我尝试在 textarea 上使用(keydown.enter)="$event.preventDefault()"但它仅在 cursor 在 textarea 中时防止换行。 How do I ensure line breaks are not added into the textarea on focus?如何确保换行符不会添加到焦点文本区域中?

Hope I understand your requirements exactly.希望我能准确理解您的要求。

Two things we need to achieve for your requirement.我们需要满足您的要求的两件事。

1, disabled press enter in textarea 1、禁止在textarea中按回车

$('textarea').keypress(function(e){
    if ( e.which == 13 ) e.preventDefault();
});

2, fire press shift key, we implement by focus next element 2、按下shift键,我们实现focus下一个元素

$("#next_element").focus();

final code like below最终代码如下

 $('textarea').keypress(function(e){ if ( e.which == 13 ){ $("#next_element").focus(); e.preventDefault(); } });
 <html> <head> <title>Textarea</title> </head> <body> <form action="javascript:void(0);" method="post"> <textarea name="description" placeholder="Description" required></textarea> <input type="text" id="next_element" placeholder="Next element" required /> <input type="submit" /> </form> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM