简体   繁体   English

javascript获取下拉列表中值的索引

[英]javascript get index of a value in the dropdownlist

I am passing a value to the dropdownlist, how to get the index of the value of the dropdownlist and set it using javascript? 我正在将值传递给dropdownlist,如何获取dropdownlist的值的索引并使用javascript进行设置?

 document.getElementById("ddlColors").selectedIndex = ?

Thank you 谢谢

A loop is the only way, really: 实际上,循环是唯一的方法:

var options = document.getElementById('ddlColors').options;

for(var i = 0; i < options.length; i++) {
    if(options[i].value === someValue) {
        options[i].selected = true;
        break;
    }
}

Here's a demo. 这是一个演示。

我想我误解了您的问题原因,如果我的答案不是很正确:请使用值http://jsbin.com/urezor/2/edit

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

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