Javascript复选框的问题
嗨,我在尝试获取复选框值时遇到一些问题。对于一个学校项目,我有一张比萨饼订单,我想打印一份订单摘要。我在打印浇头时遇到问题Javascript复选框的问题,javascript,html,webforms,checkbox,Javascript,Html,Webforms,Checkbox,嗨,我在尝试获取复选框值时遇到一些问题。对于一个学校项目,我有一张比萨饼订单,我想打印一份订单摘要。我在打印浇头时遇到问题 <label><input type = "checkbox" name = "topping" id = "sausage" value = "Sausage"/> Sausage</label> <label><input type = "checkbox" name = "topping" id = "peppe
<label><input type = "checkbox" name = "topping" id = "sausage" value = "Sausage"/> Sausage</label>
<label><input type = "checkbox" name = "topping" id = "pepperoni" value = "Pepperoni"/> Pepperoni</label>
<label><input type = "checkbox" name = "topping" id = "beef" value = "Beef"/> Beef</label>
<label><input type = "checkbox" name = "topping" id = "bacon" value = "Bacon"/> Bacon</label><br />
<label><input type = "checkbox" name = "topping" id = "chicken" value = "Chicken"/> Chicken</label>
<label><input type = "checkbox" name = "topping" id = "ham" value = "Ham"/> Ham</label>
<label><input type = "checkbox" name = "topping" id = "olives" value = "Olives"/> Olives</label>
<label><input type = "checkbox" name = "topping" id = "peppers" value = "Peppers"/> Peppers</label><br />
<label><input type = "checkbox" name = "topping" id = "tomatoes" value = "Tomatoes"/> Tomatoes</label>
<label><input type = "checkbox" name = "topping" id = "mushrooms" value = "Mushrooms"/> Mushrooms</label>
<label><input type = "checkbox" name = "topping" id = "pineapple" value = "Pineapple"/> Pineapple</label>
香肠
意大利 辣味 香肠
牛肉
培根
鸡
火腿
橄榄
辣椒
西红柿
蘑菇
菠萝
这就是html部分,我认为问题出在javascript函数中
function toppings(inputCollection) {
var toppings = "";
for (var i = 0; i < inputCollection.length; i++) {
if (inputCollection[i].checked) {
toppings = toppings + inputCollection[i].value + " ";
}
}
return toppings;
}
函数顶部(inputCollection){
var toppings=“”;
对于(var i=0;i
我对javascript相当陌生,所以如果我犯了一个愚蠢的错误,请不要解雇我。非常感谢您如何调用您的函数
这应该可以做到-toppings(document.getElementsByName(“topping”)看看这个例子,它还显示了标签是如何正确使用的:
数组inputCollection的值是多少?JS看起来应该可以工作,但我认为要真正工作,您需要通过将topping输入的
name
属性设置为toppings[]
使其成为一个数组。由于它们都被命名为topping
,现在只有最后一个(?)将被发送到服务器。但不确定这是否对JS有帮助。@powerbuoy:只有表单提交给PHP时才需要“[]”后缀约定。这是PHP的一个局限性;浏览器、Javascript和大多数服务器都不在乎,我已经弄明白了。我把它分配给一个变量,这个变量也叫做toppings。谢谢