通过AJAX调用页面后台代码方法实现省级三级联动效果 (简单练习)前台

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 <script src="jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">      
     $(function () {
         $("#ProvinceList").bind("keyup change", function (e) {             
        e.preventDefault();                // 首先初始化              
        $("#CityList").empty().append($("<option></option>").val("0").html("  ---  请选择  ---  "));            
            if ($(this).val() != "0") {                   
            sendData($(this).val());              
             }
    });

    $("#CityList").bind("keyup change", function (e) {
        e.preventDefault();                // 首先初始化              
        $("#Dist").empty().append($("<option></option>").val("0").html("  ---  请选择  ---  "));
        if ($(this).val() != "0") {
            sendData2($(this).val());
        }
    });               
                function sendData(sBuyID) {            
                             
                     $.ajax({                  
                      type: "POST",                  
                       url: "Default3.aspx/GetSubList", // 调动后台页面方法                 
                         data: '{"sBuyID":"' + sBuyID + '"}',                   
                         contentType: "application/json; charset=utf-8",                  
                          dataType: "json",                  
                           success: function (msg) {                      
                            // msg.d是数组,由后台数组ArrayList返回,因此可以遍历每个元素                      
                             $.each(msg.d, function () {
                                 // this.Value和this.Text是后台返回数组ArrayList类型包含元素ListItem类型的属性                          
                                 $("#CityList").append($("<option></option").val(this.Value).html(this.Text));
                             });
                         },
                         error: function () {
                             alert("ajax请求发生错误");
                         }
                     });
                 }

                 function sendData2(sdrID) {

                     $.ajax({
                         type: "POST",
                         url: "Default3.aspx/GetdrList", // 调动后台页面方法                 
                         data: '{"sdrID":"' + sdrID + '"}',
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                         success: function (msg) {
                             // msg.d是数组,由后台数组ArrayList返回,因此可以遍历每个元素                      
                             $.each(msg.d, function () {
                                 // this.Value和this.Text是后台返回数组ArrayList类型包含元素ListItem类型的属性                          
                                 $("#Dist").append($("<option></option").val(this.Value).html(this.Text));
                             });
                         },
                         error: function () {
                             alert("ajax请求发生错误");
                         }
                     });
                 }
             }); 
                                    
                                       </script>
</head>
<body>  
 <form id="form1" runat="server">  
  <div align="center">      
   <fieldset style="width: 480px; height: 150px;">          
    <table border="0" cellpadding="10" cellspacing="10">              
     <tr>                  
      <td>                       
      <asp:DropDownList ID="ProvinceList" runat="server" Width="120px">                           
      <asp:ListItem Value="0" Text="  --- 请选择省份 ---  "></asp:ListItem>                          
       <asp:ListItem Value="1" Text="河北"></asp:ListItem>                           
      <%-- <asp:ListItem Value="2" Text="江苏"></asp:ListItem> --%>                      
       </asp:DropDownList>                 
         </td>                  
          <td>                      
           <asp:DropDownList ID="CityList" runat="server" Width="120px">                         
             <asp:ListItem Value="0" Text="  --- 请选择城市 ---  "></asp:ListItem>                      
              </asp:DropDownList>                 
                </td> 
                      <td>                      
           <asp:DropDownList ID="Dist" runat="server" Width="120px">                         
             <asp:ListItem Value="0" Text="  --- 请选择乡镇 ---  "></asp:ListItem>                      
              </asp:DropDownList>                 
                </td>         
                 </tr>           
                 </table>       
                 </fieldset>  
                  </div>   
                  </form>
                  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值