2015-05-14 94 views
0

我有一个select的文本,需要找到相应的选项值。 我需要去,因为你要求的项目不选择通过文本查找选项值

for (var i = 0; i < combo.length; i = i + 1) { 
    if (combo.options.text == text){ // if the text value of the combo equals my variable 
     var pref = combo.options[combo.selectedIndex].value; 
     alert(pref); 
    } 
} 

回答

3

需要指数比较选项选择i

for (var i = 0; i < combo.options.length; i++) { 
    if (combo.options[i].text == text) { 
     var pref = combo.options[i].value; 
     alert(pref); 
    } 
} 

演示:Fiddle