[removed] set dropdown selected item based on option text

前端 未结 3 2083
醉梦人生
醉梦人生 2020-12-12 22:23

say I have a dropdown list like this:


                        
    
提交评论

  • 2020-12-12 22:34
    var textToFind = 'Google';
    
    var dd = document.getElementById('MyDropDown');
    for (var i = 0; i < dd.options.length; i++) {
        if (dd.options[i].text === textToFind) {
            dd.selectedIndex = i;
            break;
        }
    }
    
    0 讨论(0)
  • 2020-12-12 22:41

    You can loop through the select_obj.options. There's a #text method in each of the option object, which you can use to compare to what you want and set the selectedIndex of the select_obj.

    0 讨论(0)
  • 提交回复
    热议问题