Jquery ajax下拉框联动(用xml存放数据)

demo.jsp


  1. <%@ page language= "java"   import = "java.util.*"  pageEncoding= "utf-8" %>  
  2. <html>  
  3. <head>  
  4. <title>jquery xml解析</title>  
  5. <script type="text/javascript"  src= "js/jquery-1.3.2.js" ></script>  
  6. <script type="text/javascript" >  
  7.     $(document).ready(  
  8.             function() {  
  9.                 $.ajax( {  
  10.                     url : "xx.xml" ,  
  11.                     success : function(xml) {  
  12.                         $(xml).find("province" ).each(function() {  
  13.                             var t = $(this ).attr( "name" ); //this->      
  14.                                 $("#DropProvince" ).append(  
  15.                                         "<option>"  + t +  "</option>" );  
  16.                             });  
  17.                     }  
  18.                 });  
  19.                 $("#DropProvince" ).change(  
  20.                         function() {  
  21.                             $("#sCity>option" ).remove();  
  22.                             var pname = $("#DropProvince" ).val();  
  23.                             $.ajax( {  
  24.                                 url : "xx.xml" ,  
  25.                                 success : function(xml) {  
  26.                                     $(xml).find(  
  27.                                             "province[name='"  + pname  
  28.                                                     + "']>city" ).each(  
  29.                                             function() {  
  30.                                                 $("#sCity" ).append(  
  31.                                                         "<option>"   
  32.                                                                 + $(this )  
  33.                                                                         .text()  
  34.                                                                 + "</option>" );  
  35.                                             });  
  36.                                 }  
  37.                             });  
  38.                         });  
  39.             });  
  40. </script>  
  41. </head>  
  42. <body>  
  43. <form id="form1" >  
  44. <div>  
  45.     <select id="DropProvince"  style= "width: 60px;" >  
  46.         <option>请选择</option>  
  47.     </select>  
  48.     <select id="sCity"  style= "width: 60px;" >  
  49.     </select>  
  50. </div>  
  51. </form>  
  52. </body> 
  53. </html>

  54. xx.xml

  55. <?xml version= "1.0"  encoding= "utf-8"  ?>    
  56. <provinces>    
  57.     <province name="湖北" >    
  58.         <city>武汉</city>    
  59.         <city>黄石</city>    
  60.         <city>宜昌</city>    
  61.         <city>天门</city>    
  62.     </province>    
  63.     <province name="湖南" >    
  64.         <city>邵阳</city>    
  65.         <city>长沙</city>    
  66.         <city>岳阳</city>    
  67.     </province>    
  68.     <province name="广东" >    
  69.         <city>广州</city>    
  70.         <city>深圳</city>    
  71.     </province>    
  72. </provinces> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值