Javascript-使用预设的选定选项创建下拉菜单

问题描述 投票:0回答:2

我正在使用以下代码创建新的下拉列表。该名称由页面上的多个下拉列表使用。

  var cellSel = row.insertCell(1);
  var sel1 = document.createElement('select');
  sel1.name = 'list[]';
  sel1.options[0] = new Option('1', '1');
  sel1.options[1] = new Option('2', '2');
  sel1.options[2] = new Option('3', '3');
  sel1.options[3] = new Option('4', '4');
  cellSel.appendChild(sel1);

创建后,我希望将选项3设置为选定的条目,我该怎么做?

感谢

javascript dropdown selected
2个回答
0
投票

sel1.options [3] .selected = true


0
投票
var cellSel = row.insertCell(1);
var sel1 = document.createElement('select');
sel1.name = 'list[]';
sel1.options[0] = new Option('1', '1');
sel1.options[1] = new Option('2', '2');
sel1.options[2] = new Option('3', '3', true);
sel1.options[3] = new Option('4', '4');
cellSel.appendChild(sel1);
© www.soinside.com 2019 - 2024. All rights reserved.