jquery+ajax+asp.net 三級聯動

來源:互聯網
上載者:User

DEMO

XmlDocument xmlDoc = new XmlDocument();         
        void Datea()
        {
            StringBuilder sb = new StringBuilder();
            xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
            XmlNodeList nodelist = xmlDoc.SelectNodes("area/province[@provinceID]");
            sb.Append("{Sheng:[");
            foreach (XmlNode myNode in nodelist)
            {
                string name = myNode.Attributes["province"].Value;
                sb.Append("{");
                sb.AppendFormat("Name:\"{0}\",",name);
                sb.AppendFormat("Value:{0}", myNode.Attributes["provinceID"].Value);
                sb.Append("},");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]}");
           
            Response.Write(sb.ToString());
            Response.End();
        }
        void Sheng(string name, string pic, string str)
        {
            StringBuilder sb = new StringBuilder();
            xmlDoc.Load(Server.MapPath("ChinaArea.xml"));
            sb.Append("{" + name + ":[");
            if (str == "City")
            {
                XmlNodeList nodelist = xmlDoc.SelectNodes("area/province[@provinceID=" + pic + "]/" + str + "");
                foreach (XmlNode obj in nodelist)
                {
                    string dd = obj.Attributes["City"].Value;
                    sb.Append("{");
                    sb.AppendFormat("Name:\"{0}\",", dd);
                    sb.AppendFormat("Value:{0}", obj.Attributes["CityID"].Value);
                    sb.Append("},");
                }
            }
            else if (str == "Piecearea")
            {
                XmlNodeList nodelist = xmlDoc.SelectNodes("area/province/City[@CityID=" + pic + "]/" + str + "");
                foreach (XmlNode obj in nodelist)
                {
                    string dd = obj.Attributes["Piecearea"].Value;
                    sb.Append("{");
                    sb.AppendFormat("Name:\"{0}\",", dd);
                    sb.AppendFormat("Value:{0}", obj.Attributes["PieceareaID"].Value);
                    sb.Append("},");
                }
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]}");
            Response.Write(sb.ToString());
            Response.End();

        }

 

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
    <script src="jquery.js"></script>
    <script>
    $(function(){
          $("#Button1").click(function(){
                $.ajax({
                  data:{Full:"fu"},
                  url: "/test.aspx",
                  type: "POST",                 
                  dataType:'json',
                  cache: false,
                  success:CallBack,
                  error:function(er){       
                  BackErr(er);}
                });
          });
          $("#Select1").change(function(){
                 $.ajax({
                  data:{Name:"Shi",PieceareaID:$("#Select1").val(),ct:"City"},
                  url: "/test.aspx",
                  type: "POST",                 
                  dataType:'json',
                  cache: false,
                  success:CallBacks,
                  error:function(er){       
                  BackErr(er);}
                });
          });
          $("#Select2").change(function(){
                 $.ajax({
                  data:{Name:"X",PieceareaID:$("#Select2").val(),ct:"Piecearea"},
                  url: "/test.aspx",
                  type: "POST",                 
                  dataType:'json',
                  cache: false,
                  success:CallBackx,
                  error:function(er){       
                  BackErr(er);}
                });
          });
    });
 function CallBack(r)
    {
        var jos=eval(r);
        $.each(jos.Sheng,function(i)
        {
            $("#Select1").append("<option value=\""+jos.Sheng[i].Value+"\">"+jos.Sheng[i].Name+"</option>");
        });
    }
    function CallBacks(r)
    {
     $("#Select2").removeAttr("disabled");
     $("#Select2").html("<option value=\"0\">--請選擇--</option>");
      
      var jos=eval(r);
      $.each(jos.Shi,function(i)
        {
            $("#Select2").append("<option value=\""+jos.Shi[i].Value+"\">"+jos.Shi[i].Name+"</option>");
        });
    }
     function CallBackx(r)
    {
      $("#Select2").removeAttr("disabled");
     $("#Select3").html("<option value=\"0\">--請選擇--</option>");
      
      var jos=eval(r);
      $.each(jos.X,function(i)
        {
            $("#Select3").append("<option value=\""+jos.X[i].Value+"\">"+jos.X[i].Name+"</option>");
        });
    }
    function BackErr(r)
    {
        alert("沒有子資料");
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="擷取資料"/>
   省: <select id="Select1">
        <option value="0">--請選擇--</option>
    </select>
    市: <select id="Select2" disabled=disabled>
        <option value="0">--請選擇--</option>
    </select>
    縣:<select id="Select3" disabled=disabled>
        <option value="0">--請選擇--</option>
    </select>
   <ul id="list"></ul>
    </div>
    </form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.