首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用数组根据title或alt属性将值相加

使用数组根据title或alt属性将值相加
EN

Stack Overflow用户
提问于 2010-12-01 07:02:17
回答 2查看 246关注 0票数 0

计算表单具有选项,并根据客户端输入的内容确定侧边栏中加载的产品。产品的数量直接反映了期权的价值。

所以现在每个产品都有一个总价格,总持续时间和总潜水次数,如这里所示。

代码语言:javascript
运行
复制
num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_price_' + productid).value);
nums=num1*num2;
document.getElementById('product_price_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_duration_' + productid).value);
nums=num1*num2;
document.getElementById('product_duration_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_dives_' + productid).value);
nums=num1*num2;
document.getElementById('product_dives_total_' + productid).value = nums;

num1=Number(document.getElementById('product_quantity_' + productid).value);
num2=Number(document.getElementById('product_hire_' + productid).value);
nums=num1*num2;
document.getElementById('product_hire_total_' + productid).value = nums;

所以现在我们需要一个脚本来给我们提供所有-- 'product_price_total_‘+ productid).value的总价格--另一个是总持续时间,第三个是总潜水等等。等等。

不知道怎么做,但有几个想法可能是一个数组,它只将字段与特定的alt标签或title标签相加。

谁有什么想法。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-01 08:30:30

像这样怎么样?(使用与上述Šime Vidas相同的$()函数)

代码语言:javascript
运行
复制
var productIds = {};

function product_totals(id) {
    productIds[id] = true; // store all id's as the totals are calculated
    ....
}

function totalTotals() {
    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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2010-12-02 22:23:27

非常棒的工作伙伴们,谢谢你们的帮助。非常好的团队合作,这里是完整的最终代码。

function product_analysis(地址,盒子){ if (box.checked) {

代码语言:javascript
运行
复制
$('#product_' + box.alt).load(address);

}
else {

$('#product_' + box.alt).load('http://www.divethegap.com/update/blank.html');

}
document.getElementById('product_quantity_PRI_' + box.alt).value = box.value;

};

var productIds = {};

函数product_totals(id) { productIdsid = true;//将所有id存储为合计计算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;

代码语言:javascript
运行
复制
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);

}

}

函数totalTotals() { var totalPriceTotal = 0;var totalDurationTotal = 0;var totalDivesTotal = 0;var totalHireTotal = 0;

代码语言:javascript
运行
复制
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);

}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4319613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档