简体   繁体   English

输入按键按下javascript功能

[英]enter key pressed call javascript function

I have an asp:TextBox on my page and I would like to detect the enter key to call a javascript function. 我的页面上有一个asp:TextBox,我想检测回车键来调用javascript函数。 This is what I have: 这就是我所拥有的:

Codebehind 代码隐藏

txtSearch.Attributes.Add("OnKeyPress", "ProcessKeyPressed()")

aspx page aspx页面

 function ProcessKeyPressed() {
    if (event.keyCode == 13 || event.keyCode == 10 ) {
        Search();
    }
 }

This works in chrome and IE, but not in Firefox...any idea why this might be the case? 这在chrome和IE中有效,但在Firefox中不起作用...知道为什么会这样吗?

Thanks in advance, 提前致谢,

Try: 尝试:

function ProcessKeyPressed(event) {
   if (event.keyCode == 13 || event.keyCode == 10 ) {
       Search();
   }
}

See here for more info: window.event.keyCode how to do it on Firefox? 请参阅此处以获取更多信息: window.event.keyCode如何在Firefox上执行此操作?

Code behind: 后面的代码:

txtSearch.Attributes.Add("OnKeyPress", "ProcessKeyPressed(event || window.event)")

And JS: 和JS:

function ProcessKeyPressed(event) {
    if (event.keyCode == 13 || event.keyCode == 10 ) {
        Search();
    }
 }

Based on this 基于

Demo 演示版

You may try this 你可以试试这个

function ProcessKeyPressed(e) {
    var event = e || window.event;
    var key = event.charCode || event.keyCode
    if (key == 13 || key == 10 ) {
        Search();
    }
}

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

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