0
$(document).on("click", "#subtotal", function() {       
    var total_price=0;                                        
    for (i=1; i<= clicks; i++) {
        var element = parseFloat($("#total_price_"+i).html());
        total_price += parseFloat(element);
        var brand_code = $("#brand_"+i).val();
        var price = $("#price_"+i).html();
        var quantity = $("#quantity_"+i).html();
        if (element >=1 ) {
        total_price 
        $("#total").html(total_price);
            $.ajax({
                    type: "POST",
                    url: "modules/query_jquery/product_select.php",
                   data: {"new_invoice": "new_invoice", "brand_code": brand_code, "price" : price, "quantity" : quantity , "total_price" : total_price},
                   dataType: 'json',
                   success: function (data) {
                      message = data.message;
                      window.location.href = data.url;
                    }
            });

        }

    }
    alert(message);
});

メッセージを警告しようとしていますが、定義されていませんか?ajax jqueryで定義された可変メッセージを定義できませんか?!!!

4

3 に答える 3

0

前述のように、グローバル var を ajax 成功関数内で定義しているため、これが成功しない場合、呼び出すためのこの var は存在しません。

messageそのため、ajax 呼び出しの前に安全側に定義します。

message = null;

$(document).on("click", "#subtotal", function() {       
var total_price=0;                                        
for (i=1; i<= clicks; i++) {
    var element = parseFloat($("#total_price_"+i).html());
    total_price += parseFloat(element);
    var brand_code = $("#brand_"+i).val();
    var price = $("#price_"+i).html();
    var quantity = $("#quantity_"+i).html();
    if (element >=1 ) {
    total_price 
    $("#total").html(total_price);
        $.ajax({
                type: "POST",
                url: "modules/query_jquery/product_select.php",
               data: {"new_invoice": "new_invoice", "brand_code": brand_code, "price" : price, "quantity" : quantity , "total_price" : total_price},
               dataType: 'json',
               success: function (data) {
                  message = data.message;
                  window.location.href = data.url;
                }
        });

    }

}
alert(message);
});
于 2012-12-17T12:25:45.093 に答える
0

Ajax が完了したら、アラートを実行してみてください。

  success: function (data) {
      message = data.message;
      window.location.href = data.url;
      test();
  }

  function test() {
      if (window.message) {
          alert(message);
      } else {
          alert("global message was not populated");
      }
  }
于 2012-12-17T12:23:20.520 に答える
0

AJAX リクエストは非同期で処理されます。

alert が呼び出されたとき、HTTP リクエストはまだ返されていません。

使用する

  • 非同期フラグ (http://api.jquery.com/jQuery.ajax/)
  • 電話を延期する
  • 成功メソッドからメソッドを呼び出します

ajax リクエストを延期する例:

        $(function () {
            var req1 = $.get('test1.json').success(function (data) {
                $('body').append(data.hallo);
            });
            var req2 = $.get('test2.json').success(function (data) {
                $('body').append(data.welt);
            });

            $.when(req1, req2).done(function () {
                $('body').append('<br />ajax completed');
            }).fail(function () {
                $('body').append('<br />ajax failed');
            });
        });
于 2012-12-17T12:24:29.257 に答える