简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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