简体   繁体   English

在文本区域中按Enter键时,如何实现即时文本提交?

[英]How might I implement immediate text submission when the enter key is pressed in a textarea?

Duplicate: 重复:

Submitting data from textarea by hitting “Enter”. 点击“ Enter”,从textarea提交数据。


I've got a textarea and a submit button: 我有一个文本区域和一个提交按钮:

<textarea id="chat"></textarea>
<input type="submit" value="Submit" onclick="send_msg();">

The value of the of the texarea is submitted when Enter is hit: 按下Enter键时,将提交texarea的值:

    textarea.onkeyup = function(e) {
        alert(e.keyCode);
        e = e || window.event;
        if (e.keyCode === 13) {
            send_msg();
        }
        return true;
    }

Problem is to make the textarea submit its value prior to letting the cursor to jump to the next line. 问题是让文本区域在让光标跳到下一行之前提交其值。 How's it possible to do that? 怎么可能呢?

e.preventDefault(); This text is here because stack overflow insists that there is no useful answer with less than 30 characters. 本文之所以在这里,是因为堆栈溢出要求没有少于30个字符的有用答案。 Guess how I feel about that? 猜猜我对此有何看法?

The trick is to stop the browser from executing the default behaviour on key down and key up. 诀窍是阻止浏览器在按下和按下键时执行默认行为。 To do that, simply return false in your onkeypress/onkeyrelease handlers when you detect keycode 13. 为此,只需在检测到键码13时在onkeypress / onkeyrelease处理程序中return false

One issue this ignores is users running input method (editors) -- eg. 用户忽略的一个问题是运行输入法(编辑器)的用户-例如。 non-latin text entry. 非拉丁文字输入。 The IME i am familiar with is the Kotoeri Hiragana IM on OSX, but there are numerous others both for japanese (Kotoeri alone has multiple different input modes, and there's at least one other major one called ATOK), in addition to modes for hangul, traditional and simplified chinese, as well as numerous other less well known languages. 我熟悉的IME是OSX上的Kotoeri Hiragana IM,但还有许多其他针对日语的语言(仅Kotoeri具有多种不同的输入模式,并且至少还有一个主要的称为ATOK的主要输入法),传统和简体中文,以及许多其他不太知名的语言。 And these input methods exist on all major platforms (esp. Win, Mac, and Linux). 这些输入法存在于所有主要平台(尤其是Win,Mac和Linux)上。

The problem that they introduce from the point of view of code similar to what you're attempting is that the exact physical keypresses does not necessarily correspond to the actual input that the user is entering. 从代码的角度来看,它们与您尝试的内容类似,所带来的问题是确切的物理按键并不一定与用户输入的实际输入相对应。

For example typing the sequence of characters toukyou<enter> under hiragana by default will produce the string とうきょう , note that there is no <enter> in the resultant text because enter confirms the composed text. 例如,默认情况下在平假名下键入字符toukyou<enter>的序列将产生字符串とうきょう ,请注意,结果文本中没有<enter> ,因为Enter确认了组成的文本。 But in addition to this the actual sequence of characters that appears changes as the input is typed: 但是除此之外,输入的实际字符顺序也会发生变化:

t // t
と // to
とう // tou
とうk // touk
とうky // touky
とうきょ // toukyo
とうきょう // toukyou

If memory serves when i implemented this in webkit it was necessary to make the keyCode be 229 on keyDown for all keys typed in an IME (for compat with IE) -- but i cannot recall what the behaviour of keyUp is. 如果在我在Webkit中实现此功能时有内存可用,则对于在IME中键入的所有键(与IE兼容),必须将keyCode的keyCode设置为keyDown上的229-但我无法回忆起keyUp的行为。

It's also worth noting that in some IMEs you won't necessarily receive keydowns, keypress, or keyup. 还值得注意的是,在某些IME中,您不一定会收到按键,按键或按键。 Or you will receive multiple. 否则您会收到多个。 Or they're all sent together at the end of input. 或者它们都在输入结束时一起发送。

In general this is a very... unclean... portion of the DOM event implementations currently. 通常,这是当前DOM事件实现中非常不干净的部分。

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

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