【发布时间】:2013-08-26 08:54:47
【问题描述】:
我正在处理基于 onChange 事件中的另一个选择框填充的选择框。它适用于除 IE 以外的所有浏览器。
function getVersion(str){
if (str==""){
document.getElementById("vSelect").innerHTML="<option value=''>Please Select a product</option>";
return;
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("vSelect").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","includes/version.php?product="+str,true);
xmlhttp.send();
}
它改变了这一点
<label>Version: </label>
<select id='vSelect'>
<option value=''>Please Select a product</option>
</select>
在 IE 中,当 onchange 触发时,它只会清除选择的文本。有任何想法吗?我正在尝试在不加载库的情况下执行此操作。
【问题讨论】:
-
尽量不要使用
innerHTML,因为innerHTML太可怕了。相反,创建一个<option>元素并添加它:var newOption = document.createOption('option'); newOption.appendChild(document.createTextNode('Please select a product')); document.getElementById('vSelect').appendChild(newOption);它可能不起作用(我无法测试它),但它不会更好地工作。 -
@minitech 看看那个,谢谢 :-)
标签: javascript html internet-explorer