有没有办法打印签入表中的多个复选框的值而不覆盖另一个?
我想编写一个程序,要求提供披萨上的配料,并且我有 5 个配料,它们是复选框项目。我正在使用 getElementById 将结果打印到表内名为 t_options 的 id,我尝试使用 .innerhtml 来等于引用的文本,但这会覆盖打印的语句而不显示多个语句。我已经尝试在单个 if 结构中使用多个 topping 变量,但它也不起作用。
这是我当前用于复选框的代码。
var t1 = document.forms["order"]["topping1"].checked;
var t2 = document.forms["order"]["topping2"].checked;
var t3 = document.forms["order"]["topping3"].checked;
var t4 = document.forms["order"]["topping4"].checked;
var t5 = document.forms["order"]["topping5"].checked;
if (t1 == true) {
top = top + 1.5;
document.getElementById("t_options").innerHTML = "Pepperoni";
} else
top = top;
if (t2 == true) {
top = top + 1.5;
document.getElementById("t_options").innerHTML = "Sausage";
} else
top = top;
if (t3 == true) {
top = top + 1.5;
} else
top = top;
if (t4 == true) {
top = top + 1.5;
} else
top = top;
if (t5 == true) {
top = top + 1.5;
} else
top = top;
document.getElementById("t_result").innerHTML = "$ " + top;
table,
th,
td {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="order" name="order">
<label for="first_last"> Name:</label>
<input type="text" name="first_last" id="first_last" placeholder="First Last"> <br>
<p> Please choose your size of pizza:</p>
<input type="radio" name="size" id="s1" value="Small"> Small - $8</input><br>
<input type="radio" name="size" id="s2" value="Medium"> Medium - $10</input><br>
<input type="radio" name="size" id="s3" value="Large"> Large - $12</input><br>
<input type="radio" name="size" id="s4" value="X-Large"> Extra Large - $14</input><br>
<p>Please choose your topping ($1.50 each): </p>
<input type="checkbox" name="topping1" id="topping1" value="pepperoni"> Pepperoni </input><br>
<input type="checkbox" name="topping2" id="topping2" value="sausage"> Sausage </input><br>
<input type="checkbox" name="topping3" id="topping3" value="bacon"> Bacon </input><br>
<input type="checkbox" name="topping4" id="topping4" value="onions"> Onions </input><br>
<input type="checkbox" name="topping5" id="topping5" value="spinach"> Spinach </input><br> <br>
<select onclick="getPizza(this)" name="pick_deliv" id="select_id">
<option id="pick_deliv" value="Pickup">Pick up </option>
<option value="Delivery">Delivery </option>
</select> <br> <br>
</form>
<button onclick="getPizza()" id="btn1"> Confirm Order</button>
<h1 id="name_result"> Your Order </h1> <br> <br>
<table style="width:50%">
<tr>
<th>Description</th>
<th>Option</th>
<th>Price</th>
</tr>
<tr>
<td> Size </td>
<td id="s_result"> </td>
<td id="p_result"> </td>
</tr>
<tr>
<td> Toppings </td>
<td id="t_options"> </td>
<td id="t_result"> </td>
</tr>
<tr>
<td> Pick-Up/Delivery</td>
<td id="sel_opt"> </td>
<td id="sel_price"> </td>
</tr>
</table>
<h4 id="total_result">Your Current Total is $ </h4>
<p id="demo"> </p>
</body>
</html>
I have a program I want to write that asks for the toppings on a pizza and I have 5 toppings that are checkbox items. I am using getElementById to print the result to an id called t_options inside the table, I have tried useing .innerhtml to equal quoted text but that will overwrite the printed statement without displaying more than one. I already tried using more than topping variable inside a single if structure and it did not work either.
Here is the code I am currently using for the checkboxes.
var t1 = document.forms["order"]["topping1"].checked;
var t2 = document.forms["order"]["topping2"].checked;
var t3 = document.forms["order"]["topping3"].checked;
var t4 = document.forms["order"]["topping4"].checked;
var t5 = document.forms["order"]["topping5"].checked;
if (t1 == true) {
top = top + 1.5;
document.getElementById("t_options").innerHTML = "Pepperoni";
} else
top = top;
if (t2 == true) {
top = top + 1.5;
document.getElementById("t_options").innerHTML = "Sausage";
} else
top = top;
if (t3 == true) {
top = top + 1.5;
} else
top = top;
if (t4 == true) {
top = top + 1.5;
} else
top = top;
if (t5 == true) {
top = top + 1.5;
} else
top = top;
document.getElementById("t_result").innerHTML = "$ " + top;
table,
th,
td {
border: 1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form id="order" name="order">
<label for="first_last"> Name:</label>
<input type="text" name="first_last" id="first_last" placeholder="First Last"> <br>
<p> Please choose your size of pizza:</p>
<input type="radio" name="size" id="s1" value="Small"> Small - $8</input><br>
<input type="radio" name="size" id="s2" value="Medium"> Medium - $10</input><br>
<input type="radio" name="size" id="s3" value="Large"> Large - $12</input><br>
<input type="radio" name="size" id="s4" value="X-Large"> Extra Large - $14</input><br>
<p>Please choose your topping ($1.50 each): </p>
<input type="checkbox" name="topping1" id="topping1" value="pepperoni"> Pepperoni </input><br>
<input type="checkbox" name="topping2" id="topping2" value="sausage"> Sausage </input><br>
<input type="checkbox" name="topping3" id="topping3" value="bacon"> Bacon </input><br>
<input type="checkbox" name="topping4" id="topping4" value="onions"> Onions </input><br>
<input type="checkbox" name="topping5" id="topping5" value="spinach"> Spinach </input><br> <br>
<select onclick="getPizza(this)" name="pick_deliv" id="select_id">
<option id="pick_deliv" value="Pickup">Pick up </option>
<option value="Delivery">Delivery </option>
</select> <br> <br>
</form>
<button onclick="getPizza()" id="btn1"> Confirm Order</button>
<h1 id="name_result"> Your Order </h1> <br> <br>
<table style="width:50%">
<tr>
<th>Description</th>
<th>Option</th>
<th>Price</th>
</tr>
<tr>
<td> Size </td>
<td id="s_result"> </td>
<td id="p_result"> </td>
</tr>
<tr>
<td> Toppings </td>
<td id="t_options"> </td>
<td id="t_result"> </td>
</tr>
<tr>
<td> Pick-Up/Delivery</td>
<td id="sel_opt"> </td>
<td id="sel_price"> </td>
</tr>
</table>
<h4 id="total_result">Your Current Total is $ </h4>
<p id="demo"> </p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要覆盖每个复选框的
t_options
,而是用+=
附加到它。Instead of overwriting
t_options
for each checkbox, append to it with+=
.巴马尔已经回答了问题的要点。但是,您还应该遵循 DRY(不要重复自己)原则:
Barmar already answered the key points of the question. However, you should also follow the DRY (don't repeat yourself) principle: