gpt4 book ai didi

javascript - 复选框/单选按钮 - 添加所选项目的总价

转载 作者:行者123 更新时间:2023-11-29 18:26:46 25 4
gpt4 key购买 nike

我目前正在使用单选按钮和复选框。在这个示例项目中,我有单选按钮和复选框,根据用户选择的值显示图像。

如何为所选的每个项目添加值(value)价格并在底部显示总成本? EXAMPLE

下面的 js 显示了我根据选择的值显示图像的过程。

JS

<script>
function check_value(currentElement, val, id, type) {
var el = document.getElementById("imgBox" + id);
if (val > 0 && val < 4) { //will trigger when [1,2,3]

if(currentElement.checked)
{
el.src = "images/" + type + val + ".jpg";
el.style.display = "";
}
else
{
el.src = "";
el.style.display = "none";
}
}
}
</script>

HTML

<h2>Choose a bike</h2>
<form name="builder">
<input type="radio" name="field" onclick='check_value(this, 1, 1, "bike")'/> KAWASAKI KX 450F- $8,399<br />
<input type="radio" name="field" onclick='check_value(this, 2, 1, "bike")'/> 2010 Yamaha Road Star S- $13,090<br />
<input type="radio" name="field" onclick='check_value(this, 3, 1, "bike")'/> Aprilia RSV4- $16,999<br />
</form>

<img id="imgBox1" src="#" style="display:none">

<h2>Choose a tire</h2>
<form name="tire">
<input type="radio" name="field" onclick='check_value(this, 1, 2, "tire")'/> Michelin Pilot Road 3 Tires- $299.99<br />
<input type="radio" name="field" onclick='check_value(this, 2, 2, "tire")'/> Dunlop Roadsmart Sport-Touring Tires- $154.99<br />
<input type="radio" name="field" onclick='check_value(this, 3, 2, "tire")'/> Pirelli Scorpion Trail Tires- $119.99<br />
</form>

<img id="imgBox2" src="#" style="display:none">

<h2>Choose Accesories</h2>
<form name="tire">
<input type="checkbox" name="field" onclick='check_value(this, 1, "3a", "accessories")'/> Chrome Front Plate- $246.50<br />
<input type="checkbox" name="field" onclick='check_value(this, 2, "3b", "accessories")'/> Jacket- $87.99<br />
<input type="checkbox" name="field" onclick='check_value(this, 3, "3c", "accessories")'/> Gloves- $35.99
</form>

<div id="accessories">
<img id="imgBox3a" src="#" style="display:none">
<img id="imgBox3b" src="#" style="display:none">
<img id="imgBox3c" src="#" style="display:none">
</div>

最佳答案

我会开始

  1. 删除不必要的 formTags 并将其减少到一个
  2. 更改输入名称(例如: radio 的“cylces”和“tire”以及“frontPlate”,请参见下面的代码)。
  3. 正确的函数(减少参数)并将价格设置为输入字段的值属性
  4. 添加for循环计算当前总数

我们在这里......

<head>
<title></title>
<script>
function check_value(curElem, id) {
var el = document.getElementById("img" + curElem.name);
el.src = "images/" + curElem.name + id + ".jpg";
if (!curElem.checked)
el.style.display = "none";
else
el.style.display = "block";

// calculate Total
var total = 0;
var controls = document.getElementsByTagName('input');
for (var i = 0; i < controls.length; i++) {
if ((controls[i].type === 'radio' || controls[i].type === 'checkbox') && controls[i].checked)
total = total + parseFloat(controls[i].value);
}
document.getElementById("total").innerHTML = total;
//alert("Total: " + total);
}
</script>
</head>
<body>
<form name="builder">
<h2>
Choose a bike</h2>
<input type="radio" name="Bike" value="8399" onclick='check_value(this, 1)' />
KAWASAKI KX 450F- $8,399<br />
<input type="radio" name="Bike" value="13090" onclick='check_value(this, 2)' />
2010 Yamaha Road Star S- $13,090<br />
<input type="radio" name="Bike" value="16999" onclick='check_value(this, 3)' />
Aprilia RSV4- $16,999<br />
<img id="imgBike" src="#" style="display: none" />
<h2>
Choose a tire</h2>
<input type="radio" name="Tire" value="299.99" onclick='check_value(this, 1)' />
Michelin Pilot Road 3 Tires- $299.99<br />
<input type="radio" name="Tire" value="154.99" onclick='check_value(this, 2)' />
Dunlop Roadsmart Sport-Touring Tires- $154.99<br />
<input type="radio" name="Tire" value="119.99" onclick='check_value(this, 3)' />
Pirelli Scorpion Trail Tires- $119.99<br />
<img id="imgTire" src="#" style="display: none" />
<h2>
Choose Accesories</h2>
<input type="checkbox" name="FrontPlate" value="246.50" onclick='check_value(this, "")' />
Chrome Front Plate- $246.50<br />
<input type="checkbox" name="Jacket" value="87.90" onclick='check_value(this, "")' />
Jacket- $87.99<br />
<input type="checkbox" name="Gloves" value="35.99" onclick='check_value(this, "")' />
Gloves- $35.99
<div id="accessories">
<img id="imgFrontPlate" src="#" style="display: none" />
<img id="imgJacket" src="#" style="display: none" />
<img id="imgGloves" src="#" style="display: none" />
</div>
<div id="total"></div>
</form>
</body>

尽管可能是您的答案的解决方案 - 我没有看到此代码的用途。在现实环境中,大部分数据将来自服务器生成,因此您将不得不完全重做此代码!

关于javascript - 复选框/单选按钮 - 添加所选项目的总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12341657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com