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

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

问题描述

我试图做自己的聊天...所以我有一个输入文本字段,提交按钮,甚至不提交,它只是一个按钮....所以当按下Enter键时,我需要输入字段的值显示在我的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,你不需要编程任何东西,它的默认。但是我的按钮是type =button....所以当你按enter键什么也没有发生...如何通过按Enter键触发我的按钮?

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事件处理程序并检查keycode 13( http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char -Codes-Key-Codes.aspx 具有关键代码的列表)。你必须知道如何从事件中获取密码。

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
    }
}

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

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