简体   繁体   English

按文本查找选择选项值

[英]Find select option value by text

I have the text of a select and need to find the option corresponding value. 我有一个选择的文本,需要找到该选项对应的值。 I need to go because the select the item you seek is not selected 我要走了,因为未选择要搜索的项目

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

Need to compare the option in index i 需要比较索引i的选项

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

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM