Home  >  Article  >  Web Front-end  >  jquery+json实现数据二级联动的方法_jquery

jquery+json实现数据二级联动的方法_jquery

WBOY
WBOYOriginal
2016-05-16 15:29:131434browse

本文实例讲述了jquery+json实现数据二级联动的方法。分享给大家供大家参考,具体如下:

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();
   }); 
}

后台代码:

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

希望本文所述对大家jQuery程序设计有所帮助。

Statement:
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