如何使ajax成功函数值全局化?

时间:2016-03-08 09:22:22

标签: javascript jquery ajax

我正在使用我的在线POS系统处理条形码扫描仪。我现在的问题如下:

var barcode = 0;


// handle a key value being entered by either keyboard or scanner
$("#scanInput").keypress(function (e) {

    // restart the timer
    if (timing) {
        clearTimeout(timing);
    }

    // handle the key event
    if (e.which == 13) {
        // Enter key was entered

        // don't submit the form
        e.preventDefault();

        // has the user finished entering manually?
        if ($("#scanInput").val().length >= minChars){
            userFinishedEntering = true; // incase the user pressed the enter key
           /* alert("class "+$(this).attr("class"));
            alert("ID "+$(this).attr("id"));
            alert("value "+$(this).val());
           /*get the class starts*/

            var data;
            var id = $(this).val();

             $.ajax({
                  type: "GET",
                  dataType: "html",
                  url: "fetch_class.php?id="+id,
                  data: data,
                  success: function(data) {

                    //alert("scanclass"+data);
                    barcode = data;
                    alert("inside "+barcode);//correct value

                  }

            });
            alert("outside "+barcode);
  

//我在上面的声明中说了0   //也是这个(alert("outside "+barcode);)首先在ajax函数alert("inside "+barcode);之前执行,但为什么呢?

我需要的是ajax成功函数中的条形码的相同值,也可以在其外部使用,以便我可以将该值传递给其他函数。如何使条形码全球化?

/*get the class ends*/

            inputComplete(barcode);
        }
    }
    else {
        // some other key value was entered

        // could be the last character
        inputStop = performance.now();
        lastKey = e.which;

        // don't assume it's finished just yet
        userFinishedEntering = false;

        // is this the first character?
        if (!inputStart) {
            firstKey = e.which;
            inputStart = inputStop;

            // watch for a loss of focus
            $("body").on("blur", "#scanInput", inputBlur);
        }

        // start the timer again
        timing = setTimeout(inputTimeoutHandler, 500);
    }
});

0 个答案:

没有答案