简体   繁体   English

如何捕获“模糊”或“按键”,请在javascript jquery中输入13-否则不可以

[英]How to trap “blur” or “keypress” enter 13 in javascript jquery - OR not, both

I want to capture "enter" and "blur" on a form field. 我想在表单字段上捕获“输入”和“模糊”。 If I hit "enter" and "tab", it will also trigger the blur event... Only one trigger, so "OR" not "AND. 如果我点击“ enter”和“ tab”,它也会触发模糊事件……只有一个触发器,所以“ OR”不是“ AND”。

$('#login-input-password').bind('blur keypress', function(e){
         if (e.type == 'blur' || e.keyCode == 13) {
            // do something only once, not twice
            // e.g., if I hit "[enter"] and tab to blur, I don't want it to call twice...
         }
    });

answer accepted implemented 接受的答案已实施

FUNCTION usage 功能用法

function bindTriggerEnterOrBlur(selector,myFunction)
    {
    $(selector).bind('blur keypress', function(e){
        if (e.type == 'blur' || e.keyCode == 13) {
            if (!$(selector).data('has-triggered')) {
                    $(selector).data('has-triggered', true);
                 // do something only once, not twice
                 myFunction();
                // e.g., if I hit "[enter"] and tab to blur, I don't want it to call twice...
                }
             }
        });

    $(selector).bind('focus', function(e){
            $(selector).data('has-triggered', false);
            $(selector).select();   
        });
    }

CALL to FUNCTION 调用功能

bindTriggerEnterOrBlur('#login-input-email',submitLoginEmail);

Where submitLoginEmail is the function that does something for the trigger, eg, 其中commitLoginEmail是为触发器执行某些操作的函数,例如,

function submitLoginEmail()
    {
    // submit on enter...
    var email = $("#login-input-email").val();
    if(validEmail(email))   
        {  
        submitNextLogin(); 
        }
    }

If I am getting your requirement right, you want to execute the callback only once but currently it is getting executed twice. 如果我的要求正确,那么您只想执行一次回调,但是目前执行两次。

If that is the case then you will need some way to indicate if the callback has been called already. 如果是这种情况,那么您将需要某种方式来指示是否已经调用了回调。

One way would be to use data attributes 一种方法是使用数据属性

$('#login-input-password').bind('blur keypress', function(e){
     if (e.type == 'blur' || e.keyCode == 13) {
        if (!$(this).data('done') {
            $(this).data('done', true);
         // do something only once, not twice
        // e.g., if I hit "[enter"] and tab to blur, I don't want it to call twice...
        }
     }
});

You will also need another event handler to reset the done attribute of the element 您还需要另一个事件处理程序来重置元素的done属性

$('#login-input-password').bind('focus', function(e) {
    $(this).data('done', false);
});

You are doing an OR. 您正在执行OR。 You want a XOR (Exclusive OR), which has to be done using a combination of comparisons. 您需要异或(异或),这必须使用比较组合来完成。

if (( (e.type == 'blur') && !(e.keyCode == 13)) || 
    (!(e.type == 'blur') &&  (e.keyCode == 13))) {
  // do something only once, not twice
  // e.g., if I hit "[enter"] and tab to blur, I don't want it to call twice...
}

You want to prevent the script from firing again though, so you'll need to do even more state comparison to make sure your asynchronous event handler knows that the blur or keypress events have occurred and ensure the handler doesn't run twice. 但是,您想防止脚本再次触发,因此您需要进行更多状态比较,以确保异步事件处理程序知道发生了blurkeypress事件,并确保处理程序不会运行两次。

You can also do something like: 您还可以执行以下操作:

var doo = function(e){ console.log("do..."); }

$('#wmd-input').bind('blur', doo);
$('#wmd-input').bind('keypress focus', function(e) {
      $('#wmd-input').off('blur');
    if (e.keyCode == 13) {
        doo(e);
    } else
        $('#wmd-input').bind('blur', doo);
});

And it does bind again when focus happens. 当焦点发生时,它确实再次绑定。

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

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