繁体   English   中英

使用javascript动态创建selectbox选项

[英]Dynamically creating options for selectbox using javascript

我正在使用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;
  }
}

如何将该值作为选择框的选定值? 目前它像一个选项,但我希望此选项被选择框的选择

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

请使用以下多个属性将选择框设置为多选

<select multiple>
</select>

并为以下选项将selected属性设置为true

anOption.selected = true;

小提琴

希望这会帮助你。

anOption.selected = true;

为我工作

暂无
暂无

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

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