简体   繁体   English

如何让'keydown'事件阻止'keyup'事件?

[英]how can i make a 'keydown' event block a 'keyup' event?

I have a zip code resolver and I'm using a keyup event handler to track when the length of the input reaches 5 then query the server to resolve the zip code. 我有一个邮政编码解析器,我正在使用一个keyup事件处理程序来跟踪输入的长度何时达到5,然后查询服务器以解析邮政编码。 But I want to keep the script from being unnecessarily being called, so I was wonder if there were a way to track a keydown event and stop the keyup event when there is already 5 characters in the textbox (which would indicate that a query has probably already been submitted), if there were a way to stop the keyup event handler after? 但是我想让脚本不被不必要地调用,所以我很想知道是否有办法跟踪keydown事件并在文本框中已经有5个字符时停止keyup事件(这可能表明查询可能有已经提交了),如果有办法在之后停止keyup事件处理程序? I know this is kind of a weird question so feel free to ask questions. 我知道这是一个奇怪的问题所以请随意提问。 Here kind of a layout of what I'm talking about: 这就是我所说的布局:

$('#zip_resolver').live('keydown', function(event){
    if($(this).val().length==5){
        //STOP KEYUP EVENT
    }else{
        //DO NOTHING
    }
});
$('#zip_resolver').live('keyup', function(){
    if($(this).val().length==5){
        //SEND RESOLVE REQUEST
    }
});

You can just enforce that the keydown was 4: http://jsfiddle.net/rkw79/H7PhT/1/ 您可以强制执行keydown为4: http//jsfiddle.net/rkw79/H7PhT/1/

$('#zip_resolver').live('keydown', function(event){
   wasfour = (4 == $(this).val().length);
});
$('#zip_resolver').live('keyup', function(){
    if($(this).val().length==5  && wasfour){
        //SEND RESOLVE REQUEST
    }
});

Instead of a keydown event, how about having a boolean flag that you check in keyup to decide if you have to do a resolve? 有没有一个布尔标志,而不是keydown事件,你在keyup中检查以确定你是否必须解决?

Something like: 就像是:

if (doresolve && length == 5) {
   resolve();
   doresolve = false;
} else if (!doresolve && length <> 5) {
   doresolve = true;
}

That way, when you edit the field, the resolver check is redone. 这样,当您编辑该字段时,重新解析器检查将重做。

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

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