如何使用回车键为事件处理(JavaScript的)? [英] How do I use the Enter key as an event handler (javascript)?

查看:179
本文介绍了如何使用回车键为事件处理(JavaScript的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让我自己的聊天...所以我有一个输入文本字段,提交按钮,甚至没有提交,它只是一个按钮....所以当进入关键是pressed,我需要输入字段的值出现在我的textarea(这是只读)...

im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to appear in my textarea (which is readonly)...

关注一下..让长话短说,我只想要一个基本的回车键的事件处理程序,我知道它完美地与提交按钮CUS你不需要任何东西程序,它的默认值。但我的按钮类型=按钮......所以,当你preSS进入没有任何反应...我如何引发我的按钮由pressing进入?

well look.. make long story short, i just want a basic enter key event handler, i know it works perfectly with submit buttons cus you don't need to program anything at all, its default. but my button is type="button" .... so when you press enter nothing happens... how do i trigger my button by pressing enter?

推荐答案

您可以使burron方式提出,也可以使用onkeyup事件处理程序,并检查重点code 13(<一个href=\"http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-$c$cs-Key-$c$cs.aspx\">http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-$c$cs-Key-$c$cs.aspx有重点codeS)的列表。你必须知道如何从事件的关键code。

You could make the burron type submit, or you can use the onkeyup event handler and check for keycode 13 (http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx has a list of key codes). You'll have to know how to get the keycode from the event.

编辑:一个例子

HTML

<input onkeyup="inputKeyUp(event)" ...>

普通的JavaScript:

Plain javascript:

function inputKeyUp(e) {
    e.which = e.which || e.keyCode;
    if(e.which == 13) {
        // submit
    }
}

这篇关于如何使用回车键为事件处理(JavaScript的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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