[英]When non of the options are selected for the checkboxes, how to make the value none?
I've my code below and I'm using an array here. 我下面有我的代码,我在这里使用数组。 I'm trying to print "None" value when none of the options are selected, but instead when I submit the form I have a few checkboxes selected;
当没有选择任何选项时,我尝试打印“ None”值,但是当我提交表单时,我选择了几个复选框。 it still says "None".
它仍然显示“无”。 Without the else if condition when I choose to not select one of the option, then I get nothing for the result.
当我选择不选择一个选项时,如果没有else if条件,那么结果将一无所获。
I'm not supposed to create a "None" option for the input function. 我不应该为输入函数创建一个“无”选项。
function calculatePrice() { var array = []; var extras = document.calc.extras; for (count = 0; count < extras.length; count++) { if (extras[count].checked == true) { array.push(extras[count].value); } else if (!extras[count].checked) { array = ["None"]; } } alert("(Including extras: " + array + ")"); }
<form name="calc"> <table> <tr> <td colspan="40" id="col"> <label for="extras">EXTRAS</label> <div class="left"> <input type="checkbox" name="extras" id="ac" value="A/C">A/C (+$10) <input type="checkbox" name="extras" id="work" value="Working Brakes">Working Brakes (+$100)<br> <input type="checkbox" name="extras" id="cruise" value="Cruise Control">Cruise control (+$20) <input type="checkbox" name="extras" id="seat" value="Baby Seat">Baby Seat (+$30)<br> </div> </td> </tr> <tr> <td colspan="40" id="col"> <input type="submit" onClick="calculatePrice();" value="Estimate Cost"> </td> </tr> </table> </form>
The problem is that you're replacing array
with ["None"]
every time there's an empty checkbox, not if all of the checkboxes are empty. 问题是,每当有一个空复选框时,您都用
["None"]
替换array
,而不是如果所有复选框都为空。
The simplest fix is to check after the loop if array
is empty, eg: 最简单的解决方法是在循环之后检查
array
是否为空,例如:
for (count = 0; count < extras.length; count++) {
if (extras[count].checked == true) {
array.push(extras[count].value);
}
}
if (array.length === 0) {
array = ["None"];
}
Here it is in a working snippet: 这是一个有效的代码段:
function calculatePrice() { var array = []; var extras = document.calc.extras; for (count = 0; count < extras.length; count++) { if (extras[count].checked == true) { array.push(extras[count].value); } } if (array.length === 0) { array = ["None"]; } alert("(Including extras: " + array + ")"); }
<form name="calc"> <table> <tr> <td colspan="40" id="col"> <label for="extras">EXTRAS</label> <div class="left"> <input type="checkbox" name="extras" id="ac" value="A/C">A/C (+$10) <input type="checkbox" name="extras" id="work" value="Working Brakes">Working Brakes (+$100)<br> <input type="checkbox" name="extras" id="cruise" value="Cruise Control">Cruise control (+$20) <input type="checkbox" name="extras" id="seat" value="Baby Seat">Baby Seat (+$30)<br> </div> </td> </tr> <tr> <td colspan="40" id="col"> <input type="submit" onClick="calculatePrice();" value="Estimate Cost"> </td> </tr> </table> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.