开发者

Javascript object not removing id destroying calculating form

开发者 https://www.devze.com 2023-01-28 21:53 出处:网络
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

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. 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]开发者_Go百科);

    }
    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. Depending on how you've arranged things, the deletion may therefore be happening after the product_totals(id) call. 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. Put an alert on productIds before and after the delete and I doubt you'll see the deleted key in the object.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号