简体   繁体   English

Javascript对象未删除ID破坏计算形式

[英]Javascript object not removing id destroying calculating form

The code is now up and running however the delete part of this code does not work. 该代码现已启动并正在运行,但是该代码的删除部分不起作用。 The delete part should remove the given id from the object so that it is not included in the calculation once that id and its information is loaded out. 删除部分应从对象中删除给定的ID,以便一旦加载ID及其信息后就不将其包含在计算中。 However it doesn't it still keeps it in the calculation. 但是,并非如此,它仍然保留在计算中。 I have tried several solutions around this provided by the community but nothing has worked. 我已经尝试了社区提供的一些解决方案,但是没有任何效果。 Anybody got any ideas. 任何人都有任何想法。

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);
    }

}

You mention in comments that the second function is called 您在注释中提到第二个函数被称为

...when the product loaded in the first function has finished loading... ...在第一个功能中加载的产品完成加载时...

This is just a wild guess, but without seeing more of your code, it seems your delete is happening after the .load() , but not in a callback. 这只是一个疯狂的猜测,但是在没有看到更多代码的情况下,似乎您的delete .load() 之后进行的,但没有在回调中进行。 Depending on how you've arranged things, the deletion may therefore be happening after the product_totals(id) call. 因此,根据您对事物的排列方式,删除可能会 product_totals(id)调用之后发生。 So I'd suggest rearranging as follows: 所以我建议重新安排如下:

 $('#product_' + box.alt)
   .load('http://www.divethegap.com/update/blank2.html', function(){
     delete productIds[id];
     product_totals(id);
   });

It's hard to tell what you're doing here but it's also hard to get the delete operator wrong. 很难说出您在这里做什么,但是也很难使delete运算符出错。 Put an alert on productIds before and after the delete and I doubt you'll see the deleted key in the object. 在删除之前和之后在productIds上发出警报,我怀疑您会在对象中看到已删除的密钥。

Point is though that there's probably a better way to do whatever it is you're trying to do - could you describe the operation in english? 重点是,尽管您尝试做的事情可能有更好的方法-您可以用英语来描述操作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM