简体   繁体   English

如果未选择复选框中的任何一个,则如何将其设置为none?

[英]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.

相关问题 如果选中一个,则禁用所有复选框;如果未选中,则启用 - Disable all checkboxes if one selected and enable when none selected 当“选项”不是静态时,如何引用“选择”列表的选定值? - How do I make a reference to the selected value of a “select” list when the “options” are not static? 如何使所选值与 AngularJS 中的选项值不同? - How to make selected value different than options value in AngularJS? 从选项中选择时更改选项值 - Change a options value when selected from options 如果选中其他复选框,如何创建一个未选中其他复选框的无复选框 - How to create a none checkbox that unchecked other checkboxes if it's selected 如何通过使用数组存储数据来选中复选框 - How to make selected checkboxes with using array for data 如何获取仅选中复选框的选项值? - How to get option value of only the selected checkboxes? 如何按选择的顺序获取复选框的值? - How to get the value of checkboxes in the order they are selected? 从API调用读取多选选项时,如何使用复选框预选KendoMultiselect的值 - How to Pre-select value for KendoMultiselect with checkboxes, when the multiselect options are read from an API call 当两个复选框都被选中时,需要输入部分 - Make input section required when both checkboxes are selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM