IE中,select下拉列表框无法用innerHTML渲染。。所以无法使用oselect.innerHTML=<option>text</option>,为解决这个问题,我在select下拉框为加上一个层把它包裹住,然后直接设置div的innerHTML,下面是代码
}
else if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Micorosoft.XMLHTTP");
}
else
{
alert("xmlhttp is not exist!");
}
var op=document.getElementById("province");
// var pID=op.options[op.selectedIndex].value;
for(var i=0;i<op.options.length;i++)
{
if(op.options[i].selected)
{
var pID=op.options[i].value;
}
}
var url="handler.ashx"//post提交
// var url="handler.ashx?pid="+pID//get提交 xmlhttp.open("post",url,true)
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
call_back(xmlhttp.responseText);
}
}
}
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//post提交
xmlhttp.send("pid="+pID);//post提交
// xmlhttp.send(null);//get提交
}
function call_back(ss)
{
var citys=document.getElementById("di");
di.innerHTML="";
// for(var i=citys.options.length-1;i>=0;i--)
// {
// citys.remove(i);
// }
// citys.innerHTML="<option>df</option>"//这种方法不行,在IE下无法select无法用innerHTML渲染
di.innerHTML="<select>"+ss+"</select>";
}
// --></mce:script>
</head>
<body onload="createXMLHTTPRequest()">
<select name="province" id="province" style="width:100px;" onchange="createXMLHTTPRequest()" >
<option value="1">湖北</option>
<option value="2">湖南</option>
<option value="3">广东</option>
</select>
<div id="di">
<select name="city" id="city" style="width:100px;">
</select>
</div>
</body>
下面是handler.ashx服务器端相应页面的代码
string conn = WebConfigurationManager.ConnectionStrings["con"].ConnectionString;
public void ProcessRequest (HttpContext context) {
//string pid = context.Request.QueryString["pid"];//提交
string pid = context.Request.Form["pid"];//post提交
SqlConnection con = new SqlConnection(conn);
con.Open();
string sql = "select cName from city where pID=" + pid;
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader sdr = cmd.ExecuteReader();
string output = "";
while (sdr.Read())
{
output += "<option>" + sdr[0].ToString() + "</option>";
}
context.Response.Write(output);
}
public bool IsReusable
{
get
{
return false;
}
}
}
这里遇到的问题主要是IE下select无法用innerHTML渲染的问题。。。用一个div可以解决