gpt4 book ai didi

javascript - 选中复选框时向 JavaScript 变量添加值

转载 作者:行者123 更新时间:2023-11-30 10:17:30 26 4
gpt4 key购买 nike

这是一款简单的应用程序,用于计算所选计算机硬件元素的总价。它应该与 innerHTML 一起工作并对其进行动态更改。

问题是我的代码没有任何反应,所以你可以在我的 fiddle 上检查它或者只看下面的代码。它应该改变最后一个盒子里的价格???

FIDDLE

代码:

<table style="width:230px;padding:5px;border:1px solid #f0f0f0;font-size:14px;">
<tr style="background-color:#f0f0f0;">
<th style="width:200px;text-align:left;">Elements</th>
<th align="center"></th>
</tr>
<tr style="border-bottom:1px solid #a3a3a3;">
<td>CPU unit</td>
<td align="center">&#10003;</td>
</tr>
<tr>
<tr>
<td>Motherboard</td>
<td align="center">&#10003;</td>
</tr>
<td>Graphic card</td>
<td align="center"><input type="checkbox" id="id_1" value="25" onchange="check();"></td>
</tr>
<tr>
<td>Memory chip</td>
<td align="center"><input type="checkbox" name="" value="" onchange="check();></td>
</tr>
<tr>
<td>Monitor</td>
<td align="center"><input type="checkbox" name="" value="" onchange="check();></td>
</tr>
</table>

<table style="width:220px;padding:1px;border:1px solid #f0f0f0;font-size:22px; font-weight:bold;">
<tr style="border-bottom:1px solid #a3a3a3;text-align:center;background-color:#80CCDC">

<td id="total"><script>document.getElementById('total').innerHTML = price;</script></td></tr><tr>
</table>

JavaScript

var basic = 300;
var add = 0;

function check()
{
if(document.getElementById("id_1").checked) {
add = 120;
}
if(document.getElementById("id_1").checked) {
add = 40;
}
if(document.getElementById("id_1").checked) {
add = 90;
}

}
var p = basic + add;
var price = p + " &euro;";

最佳答案

有几个问题

  • 您需要在 check 中调用 document.getElementById('total').innerHTML = price;,以便在您单击复选框时更新;
  • 不能有多个具有相同 ID 的项目。我将它们更改为 id_1、id_2、id_3
  • 您需要添加到add变量中的现有值
  • 您必须更改所有复选框的连接,而不仅仅是第一个。

将您的代码更改为以下 http://jsfiddle.net/mendesjuan/3ja4X/3/

function check() {
var basic = 300;
var add = 0;
if(document.getElementById("id_1").checked) {
add += 120;
}
if(document.getElementById("id_2").checked) {
add += 40;
}
if(document.getElementById("id_3").checked) {
add += 90;
}
var p = basic + add;
var price = p + " &euro;";
document.getElementById('total').innerHTML = price;
}

check();

要获得更简洁的代码,您可以使用以下 http://jsfiddle.net/mendesjuan/3ja4X/4/

function updateTotal(){
var basic = 300;
var add = 0;
var form = document.getElementById('form');
// Store the value for each item in the checkbox so
// you don't need to have three separate `if`s and IDs for them.
var checkboxes = form.getElementsByClassName('addon');
for (var i=0; i < checkboxes.length; i ++) {
if (checkboxes[i].checked) {
add += parseInt(checkboxes[i].value, 10);
}
}
var p = basic + add;
var price = p + " &euro;";
document.getElementById('total').innerHTML = price;
}
// Hookup handlers from JS, not in the HTML from a single
// place using event delegation
document.getElementById('form').addEventListener('change', updateTotal);

关于javascript - 选中复选框时向 JavaScript 变量添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121900/

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