[WebService] 创建option标记

dexia115 2010-04-22

 <script type="text/javascript">
    function submit(){
    if(document.getElementById("province").options[document.getElementById("province").selectedIndex].value==0){
    alert("请选择省份!");
    return false;
    }else{
    createXMLHttpRequest();
 xmlHttp.open("post","index13.jsp",true);
 xmlHttp.onreadystatechange=callback;
 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
 xmlHttp.send("province="+document.getElementById("province").options[document.getElementById("province").selectedIndex].value);
 }
}
 function callback(){
 if(xmlHttp.readyState==4){
  if(xmlHttp.status==200){
   var city_string=xmlHttp.responseText;
   var city_array=city_string.split(",");
  
   while(document.getElementById("city").options.length>0){
      document.getElementById("city").options.remove(0);
      }
      for(var j=0;j<city_array.length;j++){
   var obj=document.getElementById("city");
   obj.options=new Option(city_string,city_string);  
   alert(city_array);
   }
 } }
 }
    </script>

  </head>
 
  <body>
   <center><h2>动态加载列表框</h2></center>
   <center>省份:<select id="province" onchange="return submit()">
   <option value="0">--选择省份--</option>
   <option value="hn">河南</option>
   <option value="gd">广东</option>
   <option value="hb">湖南</option>
   <option value="js">江苏</option>
   </select>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   城市:<select id="city"></select>
   </center>
  </body>

 

Global site tag (gtag.js) - Google Analytics