我在一个下拉列表中有各种产品。如果我更换产品,价格变化和生产线成本变化与订购数量相符。为什么我的Ajax调用不检索请求的值?
问题:当我选择产品时,会对我的DB进行Ajax调用。返回正确的price
。都好。当我从下拉菜单中选择替代产品时,产品ID及其变化正确。但是,并非返回新的正确价格,price
将显示上一个查询的值。也就是说,似乎price
正在被缓存,或者阵列data[0].price
未被清除,或者Ajax调用失败。我不相信Ajax调用失败,好像我做了进一步的产品更改,我得到一个新结果,但是,再次price
值来自其前面的查询。底线,我的price
始终是一个查询背后。我曾尝试使用cache:false
,但这已经有所作为。我也尝试设置变量result
的长度为0,刚刚发送undefined
错误。
$(".product_id").on('change', function() { //THIS GETS THE VALUE OF A PRODUCT FROM THE DB
var value = parseFloat($(this).val()); // product_id
var row = $(this).parents(':eq(1)').find('input').filter(".cost");
var rowPrice = $(this).parents(':eq(1)').find('input').filter(".price");
// then we use the product_id to grab the price using AJAX //
$.ajax({
type: 'GET',
url: 'product_prices/' + value,
cache:false,
success: function(data) {
data = JSON.parse(data);
var result = data[0].price;
var price = Number(result); //.toFixed(2);
row.data('price', price); // store the row's current price in memory
rowPrice.val(price); // allocate the price to the location called rowPrice
}
});
var quantity = $(this).parents(':eq(1)').find('input').filter(".quantity").val();
if (quantity > 0){
var price = row.data('price');
var cost = (price * quantity);
cost = Number(cost); //.toFixed(2);
var displaycost = $.formatNumber(cost,{format:"#,###.00",locale:"ca"});
row.val(displaycost);
var subTotal = 0;
$("input.cost").each(function(){
var ic = $(this).val();
if (ic !==''){
var inputCost = parseFloat(ic.replace(/,/g, ''));
subTotal += inputCost;}
});
var discount_percent = $("#discount").val();
if(discount_percent ==""){
discount_percent = 0;
}
var calculations = newCalculate(subTotal, discount_percent);
setValues(calculations, $("#updateValues"));
}
});
这是否适合您?你能让我知道吗? – trincot