代码现已启动并运行,但此代码的删除部分不起作用。删除部分应该从对象中删除给定的id,以便在id及其信息被加载后它不包含在计算中。然而,它仍然没有在计算中保持它。我已经尝试了几个由社区提供的解决方案,但没有任何效果。任何人都有任何想法。
var productIds = {}
function product_analysis(address, id, box) {
productIds[id] = true; // store all id's as the totals are calculated
if (box.checked) {
$('#product_' + box.alt).load(address);
}
else {
$('#product_' + box.alt).load('http://www.divethegap.com/update/blank2.html');
(delete productIds[id]);
}
document.getElementById('product_quantity_PRI_' + box.alt).value = box.value;
};
function product_totals(id) {
productIds[id] = true; // store all id's as the totals are calculated
var quantity = $('product_quantity_' + id).value;
var price = $('product_price_' + id).value;
var duration = $('product_duration_' + id).value;
var dives = $('product_dives_' + id).value;
var hire = $('product_hire_' + id).value;
Number($('product_price_total_' + id).value = price * quantity);
Number($('product_duration_total_' + id).value = duration * quantity);
Number($('product_dives_total_' + id).value = dives * quantity);
Number($('product_hire_total_' + id).value = hire * quantity);
function $(id) {
return document.getElementById(id);
}
var totalPriceTotal = 0;
var totalDurationTotal = 0;
var totalDivesTotal = 0;
var totalHireTotal = 0;
for (var id in productIds) {
// multiply by 1 to make sure it's a number
totalPriceTotal += $('product_price_total_' + id).value*1;
totalDurationTotal += $('product_duration_total_' + id).value*1;
totalDivesTotal += $('product_dives_total_' + id).value*1;
totalHireTotal += $('product_hire_total_' + id).value*1;
}
$('GT_total_price').value = totalPriceTotal;
$('GT_total_duration').value = totalDurationTotal;
$('GT_total_dives').value = totalDivesTotal;
$('GT_total_hire').value = totalHireTotal;
function $(id) {
return document.getElementById(id);
}
}
答案 0 :(得分:1)
你在评论中提到第二个函数叫做
...当第一个功能中加载的产品加载完毕后......
这只是一个疯狂的猜测,但是如果没有看到更多代码,那么delete
似乎发生在 .load()
之后的,而不是在回调中。根据您的安排方式,删除可能会在 product_totals(id)
电话后发生。所以我建议重新安排如下:
$('#product_' + box.alt)
.load('http://www.divethegap.com/update/blank2.html', function(){
delete productIds[id];
product_totals(id);
});
答案 1 :(得分:0)
很难说你在这里做了什么,但也很难让删除操作符错误。在删除之前和之后对productId发出警报,我怀疑你会在对象中看到删除的密钥。
虽然可能有更好的方法来做你想做的事情 - 你能用英语描述一下这个操作吗?