使用jQuery的getJSON方式提交資料(後端處理類是一般處理常式) 一個級聯的操作

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   java   使用   io   for   

前端:

 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5     <title></title> 6     <script src="script/jquery-1.7.1.js"></script> 7     <script type="text/javascript"> 8         $(function () { 9             $.getJSON(‘linkage.ashx‘, { pid: 0 }, function (msg) {10                 $.each(msg.items, function (index, item) {11                     $(‘#t1‘).append(‘<option value=‘ + item.TypeId + ‘>‘ + item.TypeTitle + ‘</option>‘);12                 });13             });14             $(‘#t1‘).change(function () {15                 $.getJSON(‘linkage.ashx‘, { pid: $(‘#t1‘).val() }, function(msg) {16                     $.each(msg.items, function(index, item) {17                         $(‘#t2‘).append(‘<option value=‘ + item.TypeId + ‘>‘ + item.TypeTitle + ‘</option>‘);18                     });19                 });20             });21         });22     </script>23 </head>24 <body>25     <select id="t1"></select>26     <select id="t2"></select>27 </body>28 </html>

後端代碼:

 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Script.Serialization; 8  9 namespace _01二級聯動10 {11     /// <summary>12     /// linkage 的摘要說明13     /// </summary>14     public class linkage : IHttpHandler15     {16 17         public void ProcessRequest(HttpContext context)18         {19             context.Response.ContentType = "text/plain";20             string connStr = "server=.;database=web1;uid=sa;pwd=sa;";21             string pid = context.Request["pid"];22             string sql = "select * from typeinfo where typeparentid = " + pid + "";23             SqlDataAdapter sda = new SqlDataAdapter(sql, connStr);24             DataTable dt = new DataTable();25             sda.Fill(dt);26             List<TypeInfo> list = new List<TypeInfo>();27             foreach (DataRow row in dt.Rows)28             {29                 list.Add30                     (31                         new TypeInfo()32                         {33                             TypeId = Convert.ToInt32(row["TypeId"]),34                             TypeTitle = row["TypeTitle"].ToString(),35                             TypeParentId = Convert.ToInt32(row["TypeParentId"])36                         }37                     );38             }39             //var item = new40             //{41             //    items = list42             //};43             //等價於   new{ items = list}44             JavaScriptSerializer jsJson = new JavaScriptSerializer();45             string res = jsJson.Serialize46                 (47                     new48                     {49                         items = list50                     }51                 );52             context.Response.Write(res);53         }54 55         public bool IsReusable56         {57             get58             {59                 return false;60             }61         }62     }63 }

一個級聯的操作

使用jQuery的getJSON方式提交資料(後端處理類是一般處理常式) 一個級聯的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.