联动省市下拉框

前端之家收集整理的这篇文章主要介绍了联动省市下拉框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@jsp页面


  1. <body>
  2. <table>
  3. <Tr>
  4. <Td>
  5. 省份:<s:select list="ar" listKey="PId" listValue="PName" name="xx" id="pid" theme="simple" onchange="lisheng(this);"></s:select>&nbsp;&nbsp;城市:
  6. <select id="yy">
  7. <option value="0">---请选择---</option>
  8. </select>&nbsp;&nbsp;<input type="button" value="查询" id="se" onclick="myclick();">
  9. </Td>
  10. </Tr>
  11. </table>
  12. </body>

@H_502_1@jsp页面

@H_502_1@var xmlhttp;

@H_502_1@function getIE()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}

@H_502_1@function lisheng(obj)
{
getIE();
var url=”${pageContext.request.contextPath}/pro_allc.action?pid=”+obj.value;
xmlhttp.open(“post”,url,true);
xmlhttp.send();
xmlhttp.onreadystatechange=getBack;
}

@H_502_1@function getBack()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{

  1. //清空城市这个下拉框
  2. while(document.getElementById("yy").options.length>0)
  3. {
  4. document.getElementById("yy").removeChild(document.getElementById("yy").childNodes[0]);
  5. }
  6.  
  7.  
  8. //第二个:注意地方(s)
  9. var t= xmlhttp.responseXML.getElementsByTagName("city");
  10.  
  11. //第三个:注意
  12. for(var i=0;i<t.length;i++)
  13. {
  14. //第四个:注意地方
  15. var option=document.createElement("option");
  16. option.value=t[i].childNodes[0].childNodes[0].nodeValue;
  17. option.text =t[i].childNodes[1].childNodes[0].nodeValue;
  18. //第五个地方:注意!
  19. document.getElementById("yy").options.add(option);
  20. }
  21.  
  22. }
@H_502_1@}

@H_502_1@function myclick()
{
var pid=document.getElementById(“pid”).value;
var cid=document.getElementById(“yy”).value;
alert(pid+” “+cid);
}


@H_502_1@Action页面
// 得到省份
public String allp()
{

  1. List arx=this.dao.getAll();
  2. //构造一个"请选择"
  3. TProvince p=new TProvince();
  4. p.setPId(0);
  5. p.setPName("---请选择---");
  6.  
  7.  
  8. ar.add(p);
  9.  
  10. for (int i = 0; i < arx.size(); i++)
  11. {
  12. TProvince pp=(TProvince) arx.get(i);
  13. ar.add(pp);
  14. }
  15.  
  16.  
  17. return "myall";
  18. }
  19.  
  20. // 得到省份下的城市
  21. public String allc()
  22. {
  23.  
  24. try
  25. {
  26.  
  27. if(pid!=0)
  28. {
  29. this.arr = this.dao.getAllC(pid);
  30. // 如何构造一个下拉框(第一个注意地方:xml)
  31. HttpServletResponse response = ServletActionContext.getResponse();
  32. response.setContentType("text/xml");
  33. response.setCharacterEncoding("UTF-8");
  34. response.setHeader("Cache-Control","no-cache");
  35. PrintWriter out = response.getWriter();
  36.  
  37. out.print("<response>");
  38. for (int i = 0; i < arr.size(); i++)
  39. {
  40. TCity c=(TCity) arr.get(i);
  41.  
  42. out.print("<city>");
  43. out.print("<cid>"+c.getCId()+"</cid>");
  44. out.print("<cname>"+c.getCName()+"</cname>");
  45. out.print("</city>");
  46. }
  47. out.print("</response>");
  48.  
  49. out.flush();
  50. out.close();
  51. }
  52. else
  53. {
  54.  
  55. // 如何构造一个下拉框(第一个注意地方:xml)
  56. HttpServletResponse response = ServletActionContext.getResponse();
  57. response.setContentType("text/xml");
  58. response.setCharacterEncoding("UTF-8");
  59. response.setHeader("Cache-Control","no-cache");
  60. PrintWriter out = response.getWriter();
  61.  
  62. out.print("<response>");
  63.  
  64. out.print("<city>");
  65. out.print("<cid>0</cid>");
  66. out.print("<cname>---请选择---</cname>");
  67. out.print("</city>");
  68.  
  69. out.print("</response>");
  70.  
  71. out.flush();
  72. out.close();
  73. }
  74.  
  75.  
  76. }
  77. catch (Exception e)
  78. {
  79. e.printStackTrace();
  80. }
  81.  
  82. return null;
  83. }
  84.  
  85. ******************************************
@H_502_1@public List getAll()
{
List ar=this.getSesison().createQuery(“from TProvince”).list();
this.closeAll();
return ar;
}

  1. public List getAllC(int pid)
  2. {
  3. List ar=this.getSesison().createQuery("from TCity a where a.TProvince.PId=?").setInteger(0,pid).list();
  4. this.closeAll();
  5. return ar;
  6. }

猜你在找的Ajax相关文章