最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript get index of a value in the dropdownlist - Stack Overflow

programmeradmin1浏览0评论

I am passing a value to the dropdownlist, how to get the index of the value of the dropdownlist and set it using javascript?

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

Thank you

I am passing a value to the dropdownlist, how to get the index of the value of the dropdownlist and set it using javascript?

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

Thank you

Share Improve this question asked Sep 29, 2012 at 22:38 BenkBenk 1,3126 gold badges36 silver badges64 bronze badges 1
  • 1 Not sure what you wish to do here? Your Javascript looks OK for setting the index if ddlColors is a <select> element. – Steve Chambers Commented Sep 29, 2012 at 22:46
Add a ment  | 

2 Answers 2

Reset to default 6

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.

I think I misunderstood your question cause if I didn't the answer is too pretty forward: use the value http://jsbin./urezor/2/edit

发布评论

评论列表(0)

  1. 暂无评论