ITeye博客

向select里添加项并设置选中时的问题

zyongsheng83 · 2007-08-03 22:08 · html/css/js · Markdown

假设有:
<select name="sname">
  <option value="1">class 1</option>
  <option value="2" selected>class 2</option>
</select>

执行:
var opt = document.createElement("option");
opt.value="3";
opt.innerHTML="class 3";
document.getElementById("sname").appendChild(opt);
document.getElementById("sname").selectedIndex=2;
//这样并不能选中第三项,只有新加的项会出现这种情况

解决:
在document.sname.selectedIndex=2;之前调用一下document.getElementById("sname").focus();
或者setTimeout(0, new Function("document.getElementById('sname').selectedIndex=2;"));

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。