使用javascript在select标记内未赋值

Value not assigning inside select tag using javascript

本文关键字:赋值 javascript select 使用      更新时间:2023-09-26

我正在调用一个返回变量的javascript

options = '<option value="' + dateVal + '">' + dateText + '</option>' + '<option value="' + dateVal + '">' + dateText + '</option>';

另外,我的select元素有一个id="select value"我正在用javascript编写document.getElementById("select-value").value=options;,但它没有取任何值,也没有抛出任何错误我也试过document.getElementById("select-value").innerHTML=options;,但仍然没有运气。

用于select 的PFB我的html代码

<select name="select-name"  id="select-value" ></select>
 <script type="text/javascript">
   formatSelectedDate();
 </script>

我建议使用appendChild而不是innerHTML。

var s = document.getElementById("select-id");
var op1 = document.createElement("option");
op1.setAttribute("value",dateval);
s.appendChild(op1);
...

编辑:它在循环中看起来更好;)