欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于jquery實(shí)現(xiàn)的省市區(qū)級聯(lián)無ajax

 更新時間:2013年09月24日 09:24:44   投稿:whsnow  
省市區(qū)級聯(lián)的實(shí)現(xiàn)方法有很多,在本文為大家介紹下如何使用jquery無ajax來實(shí)現(xiàn),感興趣的朋友可以參考下,希望對大家有所幫助

希望和大家一起學(xué)習(xí),更希望能找一份好工作,我是PHP開發(fā)工程師

以下是代碼頁面

復(fù)制代碼 代碼如下:

<span rel="con_address" class="con_address">
#foreach($data in $conAddressBean.provinceCode)
<input type="hidden" rel="province" value="$data"/>
#end
#foreach($data in $conAddressBean.cityCode)
<input type="hidden" rel="city" value="$data"/>
#end
#foreach($data in $conAddressBean.countyCode)
<input type="hidden" rel="county" value="$data"/>
#end
<input id="dataCache" type="hidden"/>

<select rel="city" name="city">
<option value="-1" rel="template">請選擇</option>
</select>

<select rel="county" rel="template" name="county">
<option value="-1">請選擇</option>
</select>
</span>

以下是js代碼

其中有專門保存數(shù)據(jù)的字符串
[code]
// JavaScript Document
$(document).ready(function(){
// 設(shè)置數(shù)據(jù)緩存 使用HashMap方式
cacheData(cityCountyStr);
cacheData(provinceCityStr);

$("select[rel='province']").change(dochangeSelect);
$("select[rel='city']").change(dochangeSelect);
setDefault();
});
function setDefault(){
$("[rel='con_address']").each(function(index){
//設(shè)置省的默認(rèn)參數(shù)
var currentProValue = getCurrentValue("province",index);
setValue($(this),"province",index,currentProValue);

var currentCity = getCurrentValue("city",index);
doLoadSelect($(this).find("select[rel='province']"),currentCity);

var currentCounty = getCurrentValue("county",index);
doLoadSelect($(this).find("select[rel='city']"),currentCounty);
});
}
function getCurrentValue(relName,index){
return $("[rel='con_address']").eq(0).find("[rel='"+relName+"']:hidden").eq(index).attr("value");
}
function setValue(parsent,name,index,currentValue){
$(parsent).find("select[rel='"+name+"'] > option").each(function(){
if($(this).attr("value")==currentValue){
$(this).attr("selected","selected");
return false;
}
})
}

function doLoadSelect(obj,nexeDetaultValue){
var thisName = $(obj).find("option:selected").text();
var thisValue = $(obj).find("option:selected").attr("value");
//alert("thisName="+thisName+" thisValue="+thisValue);
$(obj).nextAll().each(function(){
$(this).find("option").eq(0).nextAll().remove();
});
if(thisValue=='-1'){
return ;
}
//獲取下一級 所有的地區(qū)
var childrens = hashMap.Get(thisName);
//找到下一個select集合,按照省市區(qū) 排名的
var objThisSelect = $(obj).next().eq(0);
var template = $(objThisSelect).find("option").eq(0);
$(template).removeAttr("selected");
var childrensAttr = childrens.split(",");
var keyVal = "";
for(var i=0;i<childrensAttr.length;i++){
keyVal = childrensAttr[i].split(":");
var result = $(template).clone(true);
$(result).html(keyVal[0]);
$(result).attr("value",keyVal[1]);
if(nexeDetaultValue==keyVal[1]){
$(result).attr("selected","selected");
}
$(objThisSelect).append(result);
}
}
function dochangeSelect(){
doLoadSelect(this,"-1");
}

//將地名和數(shù)據(jù)庫id做成鍵值對,cache到HashMap中
function cacheData(datas){
var splitArr = datas.split("&");
var temp = "";
var tempArr;
for(var i=0;i<splitArr.length;i++){
temp = splitArr[i];
tempArr = temp.split("=");
hashMap.Set(tempArr[0],tempArr[1]);
}
}

相關(guān)文章

最新評論