Home > Web Front-end > JS Tutorial > body text

jquery json method to achieve secondary linkage of data_jquery

WBOY
Release: 2016-05-16 15:29:13
Original
1472 people have browsed it

The example in this article describes the method of jquery json to achieve secondary linkage of data. Share it with everyone for your reference, the details are as follows:

function GetCityInfo1() 
{ 
  $("#ddlCITY1").empty(); 
  //$("#ddlCOUNTY").empty(); 
  var strId = $("#ddlPROVINCE1").attr("value"); 
  $('#HiddenPro').val(strId); 
  $.get("../ashx/GetCityInfo.ashx",{ProID:strId,date:new Date().getTime(),proType:"getCity"},function(result) 
  { 
   $("#ddlCITY1").append($("<option></option>").val("0").html("--请选择城市--")); 
   var datas=eval(result); 
   for(var j in datas) 
   { 
    $("#ddlCITY1").append($("<option></option>").val(datas[j].ccode).html(datas[j].cityname)); 
   } 
    //获取区的信息
    //GetCountryInfo();
   }); 
}

Copy after login

Backend code:

if (context.Request.QueryString["ProID"] != null && context.Request.QueryString["proType"] != null) 
{ 
  string pcode = Convert.ToString(context.Request.QueryString.GetValues("ProID")[0]); 
  string strSQL = "select cityname,ccode from CD_CityInfo where pcode='" + pcode + "' "; 
  //执行T-SQL语句 返回DataTable 
  DataTable dt = Snell.SnCode.DataBase.SQLServerHelper.Query(strSQL).Tables[0]; 
  StringBuilder sb = new StringBuilder(); 
  sb.Append(CreateJsonParameters(dt)); 
  //根据省份编号获取信息 获取信息 
  if (sb.Length > 0) 
  { 
   context.Response.ClearContent(); 
   context.Response.ContentEncoding = System.Text.Encoding.UTF8; 
   context.Response.Write(sb.ToString()); 
   context.Response.End(); 
  } 
}
#region 根据Datatable的数据结构转换成json数据 
public string CreateJsonParameters(DataTable dt) 
{ 
 System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
 if (dt != null && dt.Rows.Count > 0) 
 { 
  sb.Append("["); 
  for (int i = 0; i < dt.Rows.Count; i++) 
  { 
   sb.Append("{"); 
   for (int j = 0; j < dt.Columns.Count; j++) 
   { 
    //如果值不是最后一个则添加逗号分隔
    if (j < dt.Columns.Count - 1) 
    { 
     sb.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\","); 
    } 
    //如果值为最后个字符则不添加逗号
    else if (j == dt.Columns.Count - 1) 
    { 
     sb.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\""); 
    } 
   } 
   //如果为最后一个值的话 则不添加逗号
   if (i == dt.Rows.Count - 1) 
   { 
    sb.Append("}"); 
   } 
   else 
   { 
    sb.Append("},"); 
   } 
  } 
  sb.Append("]"); 
  return sb.ToString(); 
 } 
 else { return null; } 
} 
#endregion

Copy after login

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!