简体   繁体   English

jQuery键盘事件在错误的输入字段上触发

[英]jQuery keyboard event fires on wrong input fields

I'm creating simple forms without direct submitting, than running script before. 与之前运行脚本相比,我正在创建不直接提交的简单表单。 So classic hitting enter will not send the form. 因此,经典点击Enter将不会发送表格。 However, since users are often hitting enter button to send a form, I create simple extension for hitting enter that is calling script when button is clicked. 但是,由于用户经常按Enter键来发送表单,因此我创建了用于单击Enter的简单扩展,即单击按钮时将调用脚本。

For example, I have a simple form: 例如,我有一个简单的表格:

<form id="testForm">
  <input type="email" id="email" />
  <input type="password" id="pass" />
  <button id="btnLogin" type="button">Login</button>
</form>

And extension I created is looking like this: 我创建的扩展名为:

(function($){
    $.fn.noEnter=function(options){
        let settings=$.extend({btn:""},options);
        function _init(){
            $(this).on("keypress",function(event){
                if(event.keyCode === 13) {
                    event.preventDefault();
                    $("#"+settings.btn).click();
                    return false;
                }
            });
        }
        this.each(function(){_init();});
        return{}
    };
}(jQuery));

Then in script I call something like this: 然后在脚本中,我这样调用:

$("#email").noEnter({btn:"btnLogin"});
$("#pass").noEnter({btn:"btnLogin"});
$("#btnLogin").click(function(){
.....
});

And this is working. 这正在工作。 Problem is that it is working twice , for each input field. 问题是 ,对于每个输入字段,它都工作两次 Looks like other input field is accepting keypress (or keydown) event. 看起来其他输入字段正在接受按键(或按键按下)事件。 Or am I missing something? 还是我错过了什么?

use event.stopPropagation(); 使用event.stopPropagation(); to prevent executing further. 以防止进一步执行。

$(this).on("keypress",function(event){
                if(event.keyCode === 13) {
                    event.stopPropagation();
                    event.preventDefault();
                    $("#"+settings.btn).click();
                    return false;
                }
            });

The problem is this is referring window object in _init function to avoid that try below code 问题是this是在_init函数中引用窗口对象,以避免尝试以下代码

(function($){
    $.fn.noEnter=function(options){
        let settings=$.extend({btn:""},options);
        var self = this;
        function _init(){
            $(self).on("keypress",function(event){
                if(event.keyCode === 13) {
                    event.preventDefault();
                    $("#"+settings.btn).click();
                    return false;
                }
            });
        }
        this.each(function(){_init();});
        return{}
    };
}(jQuery));

$("#email").noEnter({btn:"btnLogin"});
$("#pass").noEnter({btn:"btnLogin"});
$("#btnLogin").click(function(e){
    console.log('righty');
});

There is working example is jsfiddle if you want to try 如果您想尝试的话,有一个工作示例是jsfiddle

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

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