按文本查找选择选项值

时间:2015-05-14 11:38:25

标签: javascript

我有一个选择的文本,需要找到相应的选项值。 我需要去,因为没有选择你所寻找的项目

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);
    }
}

1 个答案:

答案 0 :(得分: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