2012-03-14 127 views
3

嗨我有一些問題,試圖獲得複選框的值。對於一個學校項目,我有一份比薩訂單,我打算打印他的訂單摘要。我在印刷配料時遇到問題。問題與Javascript複選框

<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,所以請不要阻止我,如果我犯了一個愚蠢的錯誤。非常感謝你

+0

什麼價值數組inputCollection? – 2012-03-14 00:24:39

+0

JS看起來應該適用於我,但我認爲爲了實際工作,您需要通過將'name'屬性設置爲'toppings []'來設置頂部輸入數組。由於它們現在都被命名爲'topping',所以只有最後一個(?)將被髮送到服務器。但不知道這是否會幫助JS。 – powerbuoy 2012-03-14 00:32:33

+0

@powerbuoy:只有當表單提交給PHP時,才需要「[]」後綴約定。這是PHP的限制;瀏覽器,Javascript和大多數服務器都不在乎。 – Josh 2012-03-14 00:35:31

回答

1

你是怎麼稱呼你的功能的?

這應該做到這一點 - 澆頭(document.getElementsByName(「一流」))

+0

我試過了。它只是清除我的表格。 – CodyK 2012-03-14 00:39:54

+0

我明白了。我將它分配給一個也被命名爲澆頭的變量。謝謝 – CodyK 2012-03-14 00:42:18