点击“Enter”键从textarea提交数据。 [英] Submitting data from textarea by hitting "Enter"

查看:440
本文介绍了点击“Enter”键从textarea提交数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JS的动态网页。有一个 textarea 和一个发送按钮,但没有表格标签。如何在 textarea 中按 Enter 时, 按钮触发并清除 textarea ?你可以使用textarea的一个keyup处理程序(尽管我会建议它不要这样做)。

解决方案

  [SomeTextarea] .onkeyup = function(e){
e = e ||事件;
if(e.keyCode === 13){
//开始提交函数
}
返回true;
}

*为什么不为此使用文本输入字段? Textarea特别适用于多行输入,单行输入的文本输入字段。使用输入处理程序,可以缩放多行输入。我记得曾经为XHR(aka AJAX)聊天应用程序使用它(因此textarea的行为类似于MSN输入区域),但使用CTRL输入键为新行重新启用多行输入。可能是你的想法?监听器将会像这样扩展:

pre $ [code> [SomeTextarea] .onkeyup = function(e){
e = e | |事件;
if(e.keyCode === 13&!e.ctrlKey){
//开始提交函数
}
返回true;
}


I have dynamic web page with JS. There is a textarea and a Send button, but no form tags. How do I make Submit button fire and the textarea get cleared when Enter is pressed in the textarea?

解决方案

You could use an keyup handler for the textarea (although I would advise against it*).

[SomeTextarea].onkeyup = function(e){
  e = e || event;
  if (e.keyCode === 13) {
    // start your submit function
  }
  return true;
 }

*Why not use a text input field for this? Textarea is escpecially suited for multiline input, a text input field for single line input. With an enter handler you criple the multiline input. I remember using it once for a XHR (aka AJAX) chat application (so the textarea behaved like an MSN input area), but re-enabled multiline input using the CTRL-enter key for new lines. May be that's an idea for you? The listener would be extended like this:

[SomeTextarea].onkeyup = function(e){
  e = e || event;
  if (e.keyCode === 13 && !e.ctrlKey) {
    // start your submit function
  }
  return true;
 }

这篇关于点击“Enter”键从textarea提交数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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