Проблемы с флажками JavaScript - PullRequest
3 голосов
/ 14 марта 2012

Привет. У меня возникли проблемы при попытке установить значения флажков. Для школьного проекта у меня есть форма заказа пиццы, и я должен напечатать сводку заказа. У меня проблемы с печатью начинки.

<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 ]

1 голос
/ 14 марта 2012

Как вы вызываете свою функцию?

Это должно сделать это - toppings (document.getElementsByName ("topping"))

0 голосов
/ 14 марта 2012

Посмотрите на этот пример, который также показывает, как правильно используются метки: http://jsbin.com/upeqam

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...