2011-09-17 79 views

回答

0
  1. 您不要使用}停止功能printColorAndGroup
  2. 對於鍵/值對而不是數組,您應該使用對象
  3. 您可能想要使用對象的文字表示法。
  4. onchange函數中,您可以通過選擇this。因此,select.options[...]將是可能的和更清潔。
  5. 除了名稱之外,您還可以使用ID,因此您無需每次都擔心[0]

修改過的版本:http://jsfiddle.net/pimvdb/RemPF/1/


這是一個文本對象符號:

var colors = { apple: "red", 
       grape: "purple", 
       milk: "white", 
       cheese: "yellow", 
       chicken: "white", 
       beef: "red" }; 

這是標識是如何工作的:

<input type="text" id="food_group" ...> 

你可以獲取與元素:

document.getElementById('food_group') 

這是如何通過選擇作品:

<select name="food" onchange="printColorAndGroup(this)"> 

與以下JavaScript:

function printColorAndGroup(select){ 
var text = select.options[select.selectedIndex].value; 
... 
+0

非常感謝你..是的,我錯過了大括號對不起。..你是非常有益的..再次感謝.. –

+0

哦,順便說一下,這似乎是不與IE瀏覽器,它在Chorme ..不知道爲什麼,可以幫助我呢?我試過在IE 9 .. –

+0

@Raam:不知道,在IE9中爲我工作。 – pimvdb

0

你錯過關閉} </script>標籤

function printColorAndGroup(){ 
    var text = document.getElementsByName('food')[0].options[document.getElementsByName('food')[0].selectedIndex].value; 
    document.getElementsByName('food_group')[0].value = groups[text]; 
    document.getElementsByName('food_color')[0].value = colors[text]; 
} 
相關問題