如何获得一个文本框内回车键触发功能,而不是第一/默认按钮 [英] How to get the Enter key within a textbox to trigger a function and not the first/default button

查看:169
本文介绍了如何获得一个文本框内回车键触发功能,而不是第一/默认按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让回车键触发某个功能时,它是pressed当某一文本框里面,不会触发第一个或默认按钮。

I am trying to get the Enter key to trigger a function when it is pressed when inside a certain textbox, and not trigger the first or default button.

您可以看到这里发生了什么的例子: http://jsfiddle.net/cutsomeat/WZ6TM/1/

You can see an example of what is happening here: http://jsfiddle.net/cutsomeat/WZ6TM/1/

如果您preSS其他键,你会得到一个警告框,关键code,但如果你preSS回车键,你会不会用钥匙code得到警告框,而是该按钮内的警告框单击事件。

If you press other keys you will get an alert box with the keycode, yet if you press the Enter key you will not get the alert box with the keycode, but rather the alert box within the button click event.

显然回车键触发是按钮。有没有办法避免这种情况,而是,捕捉到keyup事件回车键,然后触发另一个函数?

Obviously the Enter key is trigger the button. Is there a way to avoid this and instead, capture the Enter key in the keyup event, then trigger another function?

推荐答案

试试这个:

$('#myText').live("keypress", function(e) {
        if (e.keyCode == 13) {
            alert("Enter pressed");
            return false; // prevent the button click from happening
        }
});

演示

这篇关于如何获得一个文本框内回车键触发功能,而不是第一/默认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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