2009-07-29 74 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; 

他们都做同样的事情,但是。