本文介绍了AJAX调用后5键击不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个需要超过5按键击发过吗?

  $(文件)。就绪(函数(){

  $(#拉链code)。在(KEYUP功能(事件){// KEYUP功能

    如果(this.value.length == 5){//如果5时KEYUP那么火了ajax

        。VAR zicp code = $(#拉链code)VAL(); //得到拉链code的值

        $阿贾克斯({
            网址: "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip="+zicp$c$c,
            类型:获取,//通过GET方法发送
             成功:函数(结果){
                VAR解析度= results.substring((results.indexOf(< RS1>中)+ 5),results.indexOf(< / RS1>));
                VAR分裂= res.split(|);
                变种不同= [];
                $每个(分裂,功能(我报){
                    如果($。inArray(EL,不同的)=== -1)
                        distinct.push(EL);
                });
                $(#拉链code)的自动完成({来源:不同的});
              },
        });
    }

  });

});
 

它的正常工作,但有一个额外的按键或退格。

解决方案

  $(文件)。就绪(函数(){
    分裂= []; // Inialization空数组
    $(#拉链code)。自动完成({
        来源:分裂
    }); // Inialization启用的功能自动完成
    $(#拉链code)。KEYUP(函数(事件){
        如果(this.value.length == 5){


            。VAR zicp code = $(#拉链code)VAL();
            $阿贾克斯({
                网址: "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip=" + zicp code,
                类型:获取,//通过GET方法发送
                异步:假的,
                成功:函数(结果){
                    VAR解析度= results.substring((results.indexOf(< RS1>中)+ 5),results.indexOf(< / RS1>));
                    VAR分裂= res.split(|);
                    $(#拉链code)。自动完成({
                        来源:分裂
                    });
                },
            });

        }
    });
});
 

Why does this require more than 5 keystrokes before firing?

$( document ).ready(function() {

  $("#zipcode").on("keyup", function(event) { // keyup function

    if(this.value.length == 5){ // if 5 time keyup then fire the ajax 

        var zicpcode= $("#zipcode").val(); // get the value of zipcode

        $.ajax({
            url: "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip="+zicpcode,
            type: "get", //send it through get method
             success: function(results) {
                var res = results.substring((results.indexOf("<rs1>")+5),results.indexOf("</rs1>"));
                var splitted = res.split("|");
                var distinct = [];
                $.each(splitted , function(i, el){
                    if($.inArray(el, distinct ) === -1) 
                        distinct.push(el);
                });
                $("#zipcode").autocomplete({ source: distinct }); 
              },
        });
    }

  });

});

It's working fine but with one extra keystroke or backspace.

解决方案
$(document).ready(function() {
    splitted = []; // Inialization empty array 
    $("#zipcode").autocomplete({
        source: splitted
    }); //Inialization for enabling the feature of Auto-complete
    $("#zipcode").keyup(function(event) {
        if (this.value.length == 5) {


            var zicpcode = $("#zipcode").val();
            $.ajax({
                url: "http://pages.em.essilorusa.com/page.aspx?QS=773ed3059447707d2a7242227e94bba8efcc7ce6da09facd&zip=" + zicpcode,
                type: "get", //send it through get method
                async: false,
                success: function(results) {
                    var res = results.substring((results.indexOf("<rs1>") + 5), results.indexOf("</rs1>"));
                    var splitted = res.split("|");
                    $("#zipcode").autocomplete({
                        source: splitted
                    });
                },
            });

        }
    });
});

这篇关于AJAX调用后5键击不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:08