简体   繁体   English

使用javascript动态创建selectbox选项

[英]Dynamically creating options for selectbox using javascript

I am dynamically setting the values for a select box using java script 我正在使用Java脚本动态设置选择框的值

var dropDwnObj = document.getElementById("workingBranchCodeId");
dropDwnObj.options.length = 0;
var anOption = document.createElement("OPTION");
dropDwnObj.options.add(anOption) ;

for(var i = 0; i < jsonResult.subLocationList.length; i++) {
  var anOption = document.createElement("OPTION");
  dropDwnObj.options.add(anOption) ;
  if(selValue == jsonResult.subLocationList[i].displayKey) {
    anOption.innerHTML = jsonResult.subLocationList[i].displayValue;
    anOption.value = jsonResult.subLocationList[i].displayKey;
  }
}

How can I make this value as the selected value for the select box? 如何将该值作为选择框的选定值? currently it is coming like an option but i want this option to be the selected one for the select box 目前它像一个选项,但我希望此选项被选择框的选择

dropDwnObj.options [“您的选项”] .selected = true;

Please set select box as multiple selection using multiple attriute like below 请使用以下多个属性将选择框设置为多选

<select multiple>
</select>

and set selected attribute is true for options like below 并为以下选项将selected属性设置为true

anOption.selected = true;

Fiddle 小提琴

Hope this will help you. 希望这会帮助你。

anOption.selected = true; anOption.selected = true;

worked for me 为我工作

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

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