输入时未提交Textarea [英] Textarea not submitting on enter

查看:83
本文介绍了输入时未提交Textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能是多余的,但是在研究了关于stackoverflow的几乎所有相关问题之后,我仍然无法使我的问题消失".问题是这样的:

I know that this question is probably redundant, but after having looked through almost every related question on stackoverflow, I still haven't been able to make my problem "go away." The problem is this:

按键输入"时,提交文本区域内容,而不创建新行.

On key press of "enter", submit textarea contents instead of creating a new line.

这就是我正在使用的东西:
jQuery(此处类似问题的答案):

This is what I am working with:
The jQuery (an answer from a similar question on here):

  $('#message').keypress(function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13){
      $('#form').submit();
    }
  });

HTML:

<div class="message">
<form name="form" class="form" id="form" action="chatsendmesg.php" method="POST">
<textarea rows="2" name="message" id="message" class="message" placeholder="RM911 Chat" cols="26"></textarea>
<input id="submit" class="submit" type="submit" value="Send" name="submit">
</form>

即使使用此代码,按下"enter"键仍会创建新行,而不是提交行.我在这里不明白什么?是否缺少preventDefault?

Even with this code, the key press of "enter" still creates a new line instead of the submit. What on earth am I not understanding here?? Is it a lack of a preventDefault?

预先感谢, 蒂姆

推荐答案

这有效...

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script>
        $(function(){
             $("#message").keypress(function(e){
                var code = (e.keyCode ? e.keyCode : e.which);
                if (code == 13){
                      $("#form1").submit();   
                }
              });
        });
    </script>
  </head>
  <body>
    <div id="message">
      <form name="form1" class="form1" id="form1" action="chatsendmesg.php" method="post">
          <textarea rows="2" name="message" id="message" class="message" placeholder="RM911 Chat" cols="26"></textarea> 
          <input id="submit1" class="submit1" type="submit" value="Send" name="submit1" />
      </form>
    </div>
  </body>
</html>

这篇关于输入时未提交Textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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