2009-07-29 75 views
2

原型使中選擇選項,通過

使用樣機選項(國家列表)填充提供的值來選擇:

for (var i = 0; i < j.length; ++i) { 
      var opt = new Element('option', {value:j[i].country}) 
      opt.innerHTML = j[i].country_name;    
      country.appendChild(opt); 
} 

現在我需要做的選擇由值來選擇,somethhing像

function selectByValue(countryCode) { 
    // make option selected where option value == countryCode 
} 

如何用Prototype做到這一點?

回答

4
document.getElementById('country').value=countryCode 
+0

奇怪, 的document.getElementById( '國家')。值= COUNTRYCODE 警報(的document.getElementById( '國家')。值) 警報有效 「COUNTRYCODE」,但在組合框中舊的(第一)值仍選中,爲什麼? – Zelid 2009-07-29 10:31:18

7

要回答你的問題, 「豪你跟原型做」 更直接,變化:

document.getElementById('country').value=countryCode 

到...

$('country').value = countryCode; 

他們都做同樣的事情,但是。