2014-10-20 55 views
1

我敢肯定,我在这里忽略了一些东西......我有一些jQuery代码在按下表单按钮时触发。按下该按钮,查看优惠券代码并将其写入表格的div中,然后应用折扣(总价=折扣)。问题是,当我通过Firebug进行调试时,大约80%的代码工作。但是,当我运行代码时,它不起作用。就像代码运行速度太快,变量无法获得正确的信息。下面的代码:jQuery代码不更新网页

$('#coupon-submit').click(function() { 
    applyCoupon($('#coupon'), $(this)); 

    var price = $('#price-total').text(); 
    price = price.substring(5,price.length); 

    var theText = $('#fulldiscounttext').text(); // Sometimes this has a value while debugging, sometimes not 
    var discount = theText.substring(1, theText.length-4); // Takes unwanted characters out of string 

    var total = price - discount; 

    $('#price-total').text('US $ ' + total.toFixed(2)); 
    var thetotal = $('#price-total').text(); 
    $('#grandtotal').val(thetotal); 
}); 

applyCoupon()查找代码,并将其写入到DIV #fulldiscounttext。我正在尝试以折扣金额更新#price-total div。它不更新(除非我正在调试它)。

的applyCoupon()函数:

function applyCoupon(couponInput, couponButton) 
{ 
    removeInputNames(); 
    $.ajax({ 
     data: $('#orderform').serialize(), 
     type: 'POST', 
     timeout: 5000, 
     url: 'coupon.php', 
     dataType: 'text', 

     beforeSend: function(request) 
     { 
      $('#coupon-error').text(''); 
      couponButton.attr('disabled', 'disabled'); 

     }, 
     success: function(data, textStatus) 
     { 
      couponButton.removeAttr('disabled'); 

      if(data == "bad error code") 
       couponOK = 0; 
      else 
       couponOK = 1; 

      if (!couponOK) 
      { 
       var badcode = couponInput.val().toString(); 
       if (badcode.length > 0) 
       { 
        var fmt = 'You\'ve entered an invalid code.'; 
        $('#coupon-error').text(fmt); 
       } 
      } 
      else // Coupon recognized! 
      { 
       $('#total-row').before('<tr><td colspan="4">' 
        + '<div id="fulldiscounttext">' + data 
        + ' Off</div>' 
        + '</td></tr>'); 

       // New discount information; save and update totals 
       $('#discount-storage').text(data); 
       showPrice(); 
      } 
     } 
    }); 
} 
+1

邮政'applyCoupon'功能 - 它可能是一个异步调用... – tymeJV 2014-10-20 19:01:45

+0

可以告诉你的'applyCoupon'代码? – starvator 2014-10-20 19:02:07

回答

1

由于applyCoupon是一个异步函数的代码的其余部分将继续运行,而请求的进程。使用一个回调来运行代码调用完成后:

applyCoupon($('#coupon'), $(this), function() { 
    var price = $('#price-total').text(); 
    price = price.substring(5,price.length); 

    var theText = $('#fulldiscounttext').text(); // Sometimes this has a value while debugging, sometimes not 
    var discount = theText.substring(1, theText.length-4); // Takes unwanted characters out of string 

    var total = price - discount; 

    $('#price-total').text('US $ ' + total.toFixed(2)); 
    var thetotal = $('#price-total').text(); 
    $('#grandtotal').val(thetotal); 
}); 

而且applyCoupon功能:

function applyCoupon(couponInput, couponButton, callback) { 
    //AJAXy stuff 
    success: function(data, textStatus) 
    { 
     couponButton.removeAttr('disabled'); 

     if(data == "bad error code") 
      couponOK = 0; 
     else 
      couponOK = 1; 

     if (!couponOK) 
     { 
      var badcode = couponInput.val().toString(); 
      if (badcode.length > 0) 
      { 
       var fmt = 'You\'ve entered an invalid code.'; 
       $('#coupon-error').text(fmt); 
      } 
     } 
     else // Coupon recognized! 
     { 
      $('#total-row').before('<tr><td colspan="4">' 
       + '<div id="fulldiscounttext">' + data 
       + ' Off</div>' 
       + '</td></tr>'); 

      // New discount information; save and update totals 
      $('#discount-storage').text(data); 
      showPrice(); 
     } 
     callback(); //ADDED CALLBACK TO RUN ONCE THE AJAX REQUEST IS COMPLETE 
    } 
} 
+0

谢谢!一直以来都是这样愚蠢的。不知道为什么我不早点来这里。我知道jQuery足够做一些事情,但是当它变得更复杂时,我就迷路了。异步调用解释很有意义。现在我需要进一步了解回调。我感谢你的时间。 :) – 2014-10-20 19:22:52