js操作二級聯動實現代碼_javascript技巧

來源:互聯網
上載者:User
表結構
二級或多級聯動主要是以資料庫中具有父編號的表為基礎,這個也不例外
id,parent_id,name 三列。

採用js操作
先說下資料在js中的儲存方式。
主要用二維數組來儲存資料。結構如下:
a[父編號]=[[子編號1,子名稱1],[子編號2,子名稱2],[子編號3,子名稱3],……];
首先用父編號擷取所有的子資料,在把子資料的編號及名稱綁定在dropdown中

第一步 二級聯動資料(後面說這些資料怎麼得到)
複製代碼 代碼如下:

var cities=new Array();
cities['00000000-0000-0000-0000-000000000000']=[['028db215-8bd7-45ab-bbaa-1efa175c35ca','長春'],['bcb32195-2a46-4cd1-9472-6383e8fa55cc','瀋陽']];

var schools=new Array();
schools['028db215-8bd7-45ab-bbaa-1efa175c35ca']=[['5f22403a-7a59-4b7f-b62d-9451298cbd00','長春1'],['e8f0f665-9a9a-4c44-83fd-598e8a28dcd7','長春2']];
schools['bcb32195-2a46-4cd1-9472-6383e8fa55cc']=[['4a743dbf-1fdc-4907-a44e-8d169d715664','瀋陽1']];

第一級為城市資料,第二級為學校資料。
第二步 設定資料在dropdown中顯示


複製代碼 代碼如下:

//根據父編號擷取相應的子資料,並顯示在obj控制項中
//type=0 城市,1學校
//pid 父編號
//obj 要顯示資料的dropdown
function SetRegions(type,pid,obj)
{
var text="<option value='00000000-0000-0000-0000-000000000001'>請選擇</option>";
var tempArray=new Array();
if(type==0){
tempArray=cities[pid];
} else if (type==1){
tempArray=schools[pid];;
}
if(tempArray!=null)
{
for(var i=0;i<tempArray.length;i++){
text+="<option value='"+tempArray[i][0]+"'>"+tempArray[i][1]+"</option>";
}
}
$(obj).html(text);
}


第三步 城市改變的時候 選擇學校資料

複製代碼 代碼如下:

//用城市編號設定學校子資料
function CityChanged(){
SetRegions (1,$("#cities").val(),"#schools");
}


第四步 當然在修改的時候需要進行初始化,這也是以前我最頭疼的問題

複製代碼 代碼如下:

//初始化 城市 學校值
//cityId 城市編號
//schoolId 學校編號
function InitRegions(cityId,schoolId)
{
//初始化城市資料
//SetRegions(0,"00000000-0000-0000-0000-000000000000","#cities");
//尋找城市並設為預設值
$("#cities option[value="+cityId+"]").attr("selected","selected");
if(schoolId!="00000000-0000-0000-0000-000000000001"){
//初始化學校資料
SetRegions(1,cityId,"#schools");
//尋找學校並設為預設值
$("#schools option[value="+schoolId+"]").attr("selected","selected");
}
}


前台代碼
複製代碼 代碼如下:

<%--城市--%>
<select runat="server" id="cities" onchange="CityChanged();">
</select>
<%--學校--%>
<select runat="server" id="schools" >
</select>
<script type="text/javascript">
SetRegions(0,'00000000-0000-0000-0000-000000000000',"#cities");//設定城市第一級
var cityId='<asp:Literal ID="Literal_cityId" runat="server"></asp:Literal>';//初始化的城市編號
var schoolId= '<asp:Literal ID="Literal_schoolId" runat="server"></asp:Literal>';//初始化的學校編號
InitRegions(cityId,schoolId);//初始化
</script>


後台使用
後台初始化

複製代碼 代碼如下:

Literal_cityId.Text = "";//初始化城市id
Literal_schoolId.Text = "";//初始化學校id

後台擷取選擇項值

複製代碼 代碼如下:

string cityId = Request.Form["cities"] == null ? “00000000-0000-0000-0000-000000000001”: Request.Form["cities"];
string schoolId = Request.Form["schools"] == null ? “00000000-0000-0000-0000-000000000001”: Request.Form["schools"];



至此基本就完事了
現在看看如何擷取js聯動資料檔案

複製代碼 代碼如下:

/// <summary>
/// 產生地區js檔案
/// </summary>
private void CreateRegions()
{
#region 擷取地區檔案資訊
string _cities = "var cities=new Array();<br/>";
string _schools = "var schools=new Array();<br/>";
DAL.RegionInfo dalRegion = new DAL.RegionInfo();
#region 城市資訊
DataTable dtcity = dalRegion.GetList("parent_id='" + “00000000-0000-0000-0000-000000000000”+ "'").Tables[0];
_cities += "cities['" + ParentId + "']=[";
foreach (DataRow c in dtcity.Rows)
{
_cities += "['" + c["id"].ToString() + "','" + c["name"].ToString() + "'],";
#region 學校資訊
DataTable dtschool = dalRegion.GetList(" parent_id='" + c["id"] + "'").Tables[0];
if (dtschool == null || dtschool.Rows.Count == 0)
{
continue;
}
_schools += "schools['" + c["id"].ToString() + "']=[";
foreach (DataRow s in dtschool.Rows)
{
_schools += "['" + s["id"].ToString() + "','" + s["name"].ToString() + "'],";
}
_schools = _schools.TrimEnd(',') + "];<br/>";
#endregion
}
_cities = _cities.TrimEnd(',') + "];<br/>";
#endregion
Response.Write(_cities + "<br/><br/>");
Response.Write(_schools + "<br/><br/>");
#endregion
}


把頁面輸出的js聯動資料直接複製到js檔案中即可
其中也可以根據自己需要改為三級、多級聯動,都是一樣的道理。
希望能對大家有協助

聯繫我們

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