按Enter键时忽略下一行 [英] Ignore next line when enter is pressed

查看:102
本文介绍了按Enter键时忽略下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何解决方案可以忽略移动到下一行?
例如,我有一个textarea,当我点击enter,我的表单正在发送,但同时一个光标移动到下一行,我不想要。


解决方案

如果您不想要 textarea 的行为,请改用 input type ='text'



为防止输入密钥


keypress 事件和 事件如果 keyCode 13(EnterKey)


  $('#ta')。on('keypress',function e){if(e.keyCode === 13){e.preventDefault();}}) 

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script> < textarea name =id =tacols =30rows =10>< / textarea>   



小提琴演示


Is there are any solutions to ignore moving to the next line? For example, I have a textarea and when I click enter my form is being sent but at the same time a cursor is moved to the next line, that I don't want.

Edit: The problem is solved!/Проблема решена!

解决方案

If you do not want behavior of textarea, use input type='text' instead.

To prevent enter key ,

Attach keypress event and Event.preventDefault() if keyCode is 13(EnterKey)

$('#ta').on('keypress', function(e) {
  if (e.keyCode === 13) {
    e.preventDefault();
  }
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<textarea name="" id="ta" cols="30" rows="10"></textarea>

Fiddle Demo

这篇关于按Enter键时忽略下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆