Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用数组根据title或alt属性添加值_Javascript_Arrays_Forms_Math - Fatal编程技术网

Javascript 使用数组根据title或alt属性添加值

Javascript 使用数组根据title或alt属性添加值,javascript,arrays,forms,math,Javascript,Arrays,Forms,Math,计算表单具有选项,并根据客户端输入的内容确定侧边栏中加载的产品。产品的数量直接反映了期权的价值 所以现在每个产品都有一个总价格,总持续时间和总潜水次数,如图所示 num1=Number(document.getElementById('product_quantity_' + productid).value); num2=Number(document.getElementById('product_price_' + productid).value); nums=num1*num2; do

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

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

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;
所以现在我们需要一个脚本,它给出所有--“产品价格+产品ID”)值的总价格,另一个脚本给出总持续时间,第三个脚本给出总潜水等。。等等

不确定如何创建一个数组,它只添加带有特定alt标记或title标记的字段

有人有什么想法吗


谢谢

像这样的怎么样? (使用与上文相同的$()函数)


好极了,伙计们,谢谢你们的帮助。非常好的团队合作是完整的最终代码

功能产品分析(地址、方框){ 如果(复选框){

})

var productIds={}

功能产品_总计(id){ productIds[id]=true;//在计算总计时存储所有id 变量数量=$c('product_quantity_'+id.)。值; var价格=$c('product_price_'+id.)。价值; var持续时间=$c('product_duration_'+id.)。值; var dive=$c('product_dive_'+id.)。值; var hire=$c('product_hire_'+id.)。值

}
}

函数totalTotals(){ var totalPriceTotal=0; var totalDurationTotal=0; var TotalTotal=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数组?问题是,产品id由CMS id驱动,我不想列出它们,因为每次添加新产品时,也必须更改代码。除非有办法列出可能存在的所有ID。假设我们拥有所有的产品id,代码会是什么样子?谢谢注:如果您在上述代码中引用+产品id,该id来自运行此函数的单个产品代码,如“产品总数”(“PRI”);您的代码显然存在冗余问题。你知道的,对吧?你必须原谅我。如果有更好的方法,我想知道。如果您看到它的实现,可能会解决一些问题。单击初学者,然后使用复选框。不要使用太多隐藏字段,尝试将带有产品信息的JSON对象从服务器发送到UI。然后,您需要做的就是获取产品ID,查找值并进行计算。如果仍然需要将复杂数据传递回服务器,则将JSON对象传递回隐藏字段,并在服务器上解析它们。您还可以使用AJAX调用服务器方法来处理数据。您好,感谢您在上面的答案中实现的所有代码。不过,还有一个问题。如果您在上查看实现并单击初学者。一旦通过单击复选框加载了所有ajax,如果取消选中复选框,则由“test”激活的脚本将不再工作。任何想法。当我点击“测试”链接时,我得到一个错误,表示totalTotals()未定义。如果您在jQuery函数中定义了它,您可能希望将该函数绑定到您正在使用的链接。否则,它将在全局范围内不可用。因此,不要单击onclick=“totalTotals();return false”,而是在定义totalTotals:$(“#testLink”)后立即尝试此操作;我在修改代码,这就是为什么它不起作用。我相信问题在于代码计算产品ID的方式。当产品加载时(即,其选项框未选中,产品通过ajax加载),它似乎仍处于产品ID的初始计数中,因此脚本没有进行调整和重新计算。你自己试试,勾一个盒子,然后把它解开。有什么想法吗?每次取消选中某项时,您都需要从productIds对象中删除该id(删除productIds[id];),如果您使用jQuery,您只需执行$(“#GT_total_price”).value,而不必对其他项使用$c('GT_total_price')函数等等。如果使用jQuery,$c函数是多余的。
$('#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;
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);
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);