【问题标题】:Active a function when Key hit/pressed in InputBox in Javascript?在Javascript的输入框中没有/按下键时激活功能?
【发布时间】:2014-02-08 23:30:44
【问题描述】:

我希望当用户在输入框中按下 ENTER 键时,将激活一个功能。如下所示:

if (ENTERKEY is Pressed in INPUTBOX){
    Function(){....
    }
};

这是输入框:

<input class="form-control" type="text" id="second_gear"/>

点击ENTER键后触发此功能:

jump = function () {
       //do...........
       //do.....
    }

【问题讨论】:

    标签: javascript dom event-handling dom-events keyevent


    【解决方案1】:

    试试这个:

    $( "#INPUTBOX" ).focus(function() {
        function (event) {
                    if (event.which == 13 || event.keyCode == 13) {
                        //code here
                        return false;
                    }
                    return true;
                });
    

    【讨论】:

    • 当用户按 Enter 时,您的代码将起作用。但我想要的不是那个。如果 ENTER 仅在 InputBox 中被击中,我想要。
    • 我使用一点 jquery 或者如果你想要纯 javascript 使用 document.activeElement。
    猜你喜欢
    • 2010-09-27
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多