Javascript 计算通过jquery加载的值

Javascript 计算通过jquery加载的值,javascript,jquery,Javascript,Jquery,从一个已解决的案例到一个新问题的延续。下面的代码计算通过ajax从另一个脚本加载的值。另一种形式的一系列选项确定加载的信息以及最终的计算结果 问题是,一旦加载了所有可用的选项,就无法再加载它们,而将所有字段总计相加的脚本实际上在这一点上失败了。因此,例如,如果您加载2个选项,它将增加总数。再加上三分之一,这三个加起来就够了。去掉一个选项,脚本就会失败。我相信这与脚本获取产品ID的方式有关-var productID={} 任何帮助都将不胜感激 这是密码 var productIds = {};

从一个已解决的案例到一个新问题的延续。下面的代码计算通过ajax从另一个脚本加载的值。另一种形式的一系列选项确定加载的信息以及最终的计算结果

问题是,一旦加载了所有可用的选项,就无法再加载它们,而将所有字段总计相加的脚本实际上在这一点上失败了。因此,例如,如果您加载2个选项,它将增加总数。再加上三分之一,这三个加起来就够了。去掉一个选项,脚本就会失败。我相信这与脚本获取产品ID的方式有关-
var productID={}

任何帮助都将不胜感激

这是密码

var productIds = {};

function product_totals(id) {
    productIds[id] = true; // store all id's as the totals are calculated
    var quantity = $c('product_quantity_' + id).value;
    var price = $c('product_price_' + id).value;
    var duration = $c('product_duration_' + id).value;
    var dives = $c('product_dives_' + id).value;
    var hire = $c('product_hire_' + id).value;
    Number($c('product_price_total_' + id).value = price * quantity);
    Number($c('product_duration_total_' + id).value = duration * quantity);
    Number($c('product_dives_total_' + id).value = dives * quantity);
    Number($c('product_hire_total_' + id).value = hire * quantity);

    function $c(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 += $c('product_price_total_' + id).value * 1;
        totalDurationTotal += $c('product_duration_total_' + id).value * 1;
        totalDivesTotal += $c('product_dives_total_' + id).value * 1;
        totalHireTotal += $c('product_hire_total_' + id).value * 1;
    }
    $c('GT_total_price').value = totalPriceTotal;
    $c('GT_total_duration').value = totalDurationTotal;
    $c('GT_total_dives').value = totalDivesTotal;
    $c('GT_total_hire').value = totalHireTotal;

    function $c(id) {
        return document.getElementById(id);
    }
}

(单击初学者)的工作示例非常感谢

如果我理解的话,您试图实现的是将ID集合堆叠在一个数组中,您可以从中删除项目,这样您就可以只计算选定的选项。我将声明
var-productIds=[]并使用
productIds.push(id)
在其中添加id。删除选项时,可以只使用
productids.splice(indexofId,1)。当你重新查询时,你只会得到你想要的ID。

某种程度上说,不太确定这是否能做到。这些选项确定在右侧加载的内容。装载的产品包含信息(价格等)。取消选择某个选项时,其关联产品将从右侧加载。上面的脚本计算右侧的信息,但在加载产品时不会更新其ProductID集合。任何想法。我之所以告诉您在不再需要数组时使用数组并删除元素,是因为在某个时候,您迭代了ProductIds集合,并尝试使用这些ID解析卸载的控件,这会杀死您的脚本。