Javascript复选框的问题

时间:2012-03-14 00:21:05

标签: javascript html webforms checkbox

您好我在尝试获取复选框值时遇到了一些问题。对于一个学校项目,我有一个披萨订单,我想打印一份订单摘要。我在打印浇头时遇到问题。

<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;
}

我对javascript很新,所以如果我犯了一个愚蠢的错误,请不要激怒我。非常感谢你

2 个答案:

答案 0 :(得分:1)

你是如何称呼你的职能的?

这应该这样做 - 浇头(document.getElementsByName(“topping”))

答案 1 :(得分:0)

看一下这个例子,它还展示了如何正确使用标签: http://jsbin.com/upeqam