IE中,select下拉式清單方塊無法用innerHTML渲染。。所以無法使用oselect.innerHTML=<option>text</option>,為解決這個問題,我在select下拉框為加上一個層把它包裹住,然後直接設定div的innerHTML,下面是代碼
<mce:script type="text/javascript"><!--<br /> var xmlhttp;<br /> function createXMLHTTPRequest()<br /> {<br /> if(window.XMLHttpRequest)<br /> {<br /> xmlhttp=new XMLHttpRequest();</p><p> }<br /> else if(window.ActiveXObject)<br /> {<br /> xmlhttp=new ActiveXObject("Micorosoft.XMLHTTP");<br /> }<br /> else<br /> {<br /> alert("xmlhttp is not exist!");<br /> }<br /> var op=document.getElementById("province");<br />// var pID=op.options[op.selectedIndex].value;<br /> for(var i=0;i<op.options.length;i++)<br /> {<br /> if(op.options[i].selected)<br /> {<br /> var pID=op.options[i].value;<br /> }<br /> }<br /> var url="handler.ashx"//post提交<br />// var url="handler.ashx?pid="+pID//get提交 xmlhttp.open("post",url,true)<br /> xmlhttp.onreadystatechange=function()<br /> {<br /> if(xmlhttp.readyState==4)<br /> {<br /> if(xmlhttp.status==200)<br /> {<br /> call_back(xmlhttp.responseText);<br /> }<br /> }<br /> }<br /> xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//post提交<br /> xmlhttp.send("pid="+pID);//post提交<br />// xmlhttp.send(null);//get提交<br /> }<br /> function call_back(ss)<br /> {<br /> var citys=document.getElementById("di");<br /> di.innerHTML="";<br />// for(var i=citys.options.length-1;i>=0;i--)<br />// {<br />// citys.remove(i);<br />// }<br />// citys.innerHTML="<option>df</option>"//這種方法不行,在IE下無法select無法用innerHTML渲染<br /> di.innerHTML="<select>"+ss+"</select>";<br /> }</p><p>// --></mce:script><br /></head><br /><body onload="createXMLHTTPRequest()"><br /><select name="province" id="province" style="width:100px;" onchange="createXMLHTTPRequest()" ><br /> <option value="1">湖北</option><br /> <option value="2">湖南</option><br /> <option value="3">廣東</option><br /></select><br /><div id="di"><br /><select name="city" id="city" style="width:100px;"><br /></select><br /></div><br /></body>
下面是handler.ashx伺服器端相應頁面的代碼public class Handler : IHttpHandler {</p><p> string conn = WebConfigurationManager.ConnectionStrings["con"].ConnectionString;<br /> public void ProcessRequest (HttpContext context) {<br /> //string pid = context.Request.QueryString["pid"];//提交<br /> string pid = context.Request.Form["pid"];//post提交<br /> SqlConnection con = new SqlConnection(conn);<br /> con.Open();<br /> string sql = "select cName from city where pID=" + pid;<br /> SqlCommand cmd = new SqlCommand(sql,con);<br /> SqlDataReader sdr = cmd.ExecuteReader();<br /> string output = "";<br /> while (sdr.Read())<br /> {<br /> output += "<option>" + sdr[0].ToString() + "</option>";<br /> }<br /> context.Response.Write(output);</p><p> }<br /> public bool IsReusable<br /> {<br /> get<br /> {<br /> return false;<br /> }<br /> }</p><p>}
這裡遇到的問題主要是IE下select無法用innerHTML渲染的問題。。。用一個div可以解決