防止换行,但允许在textarea(jquery)中输入(提交) [英] Prevent new line but allow enter(submit) in textarea (jquery)

查看:47
本文介绍了防止换行,但允许在textarea(jquery)中输入(提交)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,我希望用户编辑已经存在的文本区域,然后按Enter键以使用Ajax调用提交,但也要在按下Enter键后禁用它进入新行.谢谢

i have a text area and i want users to edit whats already there and press enter to submit using an ajax call but also disable it going to a new line once enter is pressed. thanks

$(document).keypress(function(e) {
  if (!$(".specialClass").is(":focus")) {
    e.preventDefault();
    if (e.which == 13) {
      console.log('You pressed enter!');
    }
  }
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

推荐答案

您将使用类似以下内容的

You would use something like:

$('textarea').on('keyup keypress', function(e) {
  var keyCode = e.keyCode || e.which;
  if (keyCode === 13) { 
    e.preventDefault();

    // Ajax code here

    return false;
  }
});

这篇关于防止换行,但允许在textarea(jquery)中输入(提交)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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