select动态添加并自动选择新添加项,报错 噢,不好意思,去掉alert后确实报错 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是你执行的时机不对<html><head><script language="javascript">function test(){var oOption = document.createElement("option");oOption.appendChild(document.createTextNode("test"));oOption.setAttribute("value","test"); var sel = document.getElementById("select2");sel.appendChild(oOption);//alert(sel.options.length); //返回的数值已经包含新添加的项sel.options[sel.options.length-1].selected = true; // 报错:无法设置 selected 属性。 未指明的错误}</script></head><body ><form><select id="select2" name="select2"><option value="22">22</option></select><script>test();</script></form></body></html> 楼上的,如果是这么简单的环境就好了,我是用AJAX取回数据后动态填充的,填充后死活选择不了新加项 试过了 延迟了5秒都没效果 刚搜索到个文章,说是ALERT或LOAD后再运行添加选择和动态运行的效果完全不一样 tryvar oOption = document.createElement("option");oOption.appendChild(document.createTextNode("test"));oOption.setAttribute("value","test"); var sel = document.getElementById("select2");sel.appendChild(oOption);alert(sel.options.length); //返回的数值已经包含新添加的项//sel(sel.options.length-1).selected = true; // 报错:无法设置 selected 属性。 未指明的错误 //==>sel.options[sel.options.length-1].selected = true; //var oOption = document.createElement("option");//oOption.appendChild(document.createTextNode("test"));//oOption.setAttribute("value","test"); var oOption=new Option("test","test");var sel = document.getElementById("select2");//sel.appendChild(oOption);sel.options.add(oOption);//alert(sel.options.length); //返回的数值已经包含新添加的项//sel(sel.options.length-1).selected = true; // 报错:无法设置 selected 属性。 未指明的错误 //==>sel.options[sel.options.length-1].selected = true;try,而且你没说明你怎么使用ajax,没办法测试 setTimeout(function() { $("#name").val("11"); //指定当前值为11的这选中项; }, 1);设置一个timeout就可以了 jQuery的animate方法,貌似很卡啊。 求助这个javascript的问题 求正责表达式用于 js的验证 Extjs4.07 如何让一个msg弹出框在打开后几秒自动关闭! document.frmSearch这个具提的作用是什么? 用JS写一个简单的正则表达式 如何插入行.谢谢 跪求大虾,一直问了都没人回答,分都给完了,又申请了一个号,层的问题? 如何js写这样的代码:在一个页面中触发另一个页面的事件的!!!!!! 有没有判断一个字符串是不是数字的函数 jquery animate 问题 在线等! 修复ie6-position:fixed
<html>
<head>
<script language="javascript">
function test()
{var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode("test"));
oOption.setAttribute("value","test");
var sel = document.getElementById("select2");
sel.appendChild(oOption);
//alert(sel.options.length); //返回的数值已经包含新添加的项
sel.options[sel.options.length-1].selected = true; // 报错:无法设置 selected 属性。 未指明的错误}
</script>
</head>
<body >
<form>
<select id="select2" name="select2"><option value="22">22</option></select>
<script>test();</script></form>
</body>
</html>
var oOption = document.createElement("option");
oOption.appendChild(document.createTextNode("test"));
oOption.setAttribute("value","test");
var sel = document.getElementById("select2");
sel.appendChild(oOption);
alert(sel.options.length); //返回的数值已经包含新添加的项
//sel(sel.options.length-1).selected = true; // 报错:无法设置 selected 属性。 未指明的错误
//==>
sel.options[sel.options.length-1].selected = true;
//oOption.appendChild(document.createTextNode("test"));
//oOption.setAttribute("value","test");
var oOption=new Option("test","test");
var sel = document.getElementById("select2");
//sel.appendChild(oOption);
sel.options.add(oOption);
//alert(sel.options.length); //返回的数值已经包含新添加的项
//sel(sel.options.length-1).selected = true; // 报错:无法设置 selected 属性。 未指明的错误
//==>
sel.options[sel.options.length-1].selected = true;try,而且你没说明你怎么使用ajax,没办法测试
$("#name").val("11"); //指定当前值为11的这选中项;
}, 1);设置一个timeout就可以了