功能:省,市,地區三級聯動,采用jquery ajax 取數據綁定,頁面刷新或提交後選定值能保存並保持選中狀態
把以下代碼放在一個單獨的js文件中,在頁面上引用即可調用
。 代碼如下:
//獲取cookie值
function readCookie(name) {
var cookieValue = "";
var search = name + "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
//保存cookie值
function writeCookie(name, value, hours, escp) {
var expire = "";
if (hours != null) {
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
if (escp == "True") {
document.cookie = name + "=" + value + expire;
} else {
document.cookie = name + "=" + escape(value) + expire;
}
}
//省市區,流域,水系,斷面條件選擇
$(function() {
var $ddlprovince = $("#ddlProvince"); //省份
var $ddlcity = $("#ddlCity"); //城市
var $ddlarea = $("#ddlAreaName"); //區域
//根據省份查詢城市
//$ddlprovince.focus(); //不要加focus,否則設置selected有問題
$ddlprovince.bind("change keyup", function() {
if ($(this).val() != "-1") {
loadWater($(this).val(), "SelectCity");
} else {
$("select[id$=ddlCity] > option").remove();
$ddlcity.append($("<option></option>").val("-1").html("--請選擇--"));
}
//從新選擇省份或者從新選擇城市都會初始化區域
$("select[id$=ddlAreaName] > option").remove();
$ddlarea.append($("<option></option>").val("-1").html("--請選擇--"));
});
//如果城市沒有填充數據,省份有選擇數據則加載數據
if ($("select[id$=ddlCity] > option").length == 1 && $ddlprovince.val() != "-1") {
loadWater($ddlprovince.val(), "SelectCity");
//讀cookie,有值則設為選中狀態
var cityname = readCookie("JQ_CityName");
if (cityname != null && cityname != "undefined" && cityname != "") {
//$("select[id=ddlWaterXiName] > option:contains('" + watername + "')").attr("selected", "true");
$("select[id$=ddlCity] > option[value='" + cityname + "']").attr("selected", "true");
}
}
//根據城市查詢區域
//$ddlcity.focus();
$ddlcity.bind("change keyup", function() {
if ($(this).val() != "-1") {
loadWater($(this).val(), "SelectAreaName");
//選中值存cookie
writeCookie("JQ_CityName", $(this).val(), 0.5, true);
} else {
$("select[name$=ddlAreaName] > option").remove();
$ddlarea.append($("<option></option>").val("-1").html("--請選擇--"));
}
});
//如果區名沒有填充數據,城市有選擇數據則加載數據
if ($("select[id$=ddlAreaName] > option").length == 1 && $ddlcity.val() != "-1") {
loadWater($ddlcity.val(), "SelectAreaName");
//讀cookie,有值則設為選中狀態
var areaname = readCookie("JQ_AreaName");
if (areaname != null && areaname != "undefined" && areaname != "") {
$("select[id=ddlAreaName] > option[value='" + areaname + "']").attr("selected", "true");
}
}
$ddlarea.bind("change keyup", function() {
if ($(this).val() != "-1") {
//選中值存cookie
writeCookie("JQ_AreaName", $(this).val(), 0.5, true);
}
});
});
function loadWater(selectedItem, typename) {
$.ajax({
type: "GET",
url: "/GetWaterxiname.ashx",
data: { usetype: typename, id: selectedItem },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function Success(data) {
bindWater(data, typename);
}
});
}
function bindWater(data, typename) {
if (typename == "SelectCity") { //綁定城市
$("select[name$=ddlCity] > option").remove(); //刪除原有的option
$("select[id$=ddlCity]").append($("<option></option>").val("-1").html("--請選擇--"));
for (var i = 0; i < data.length; i++) {
$("select[id$=ddlCity]").append($("<option></option>").val(data[i].City).html(data[i].City));
}
}
else if (typename == "SelectAreaName") {
$("select[name$=ddlAreaName] > option").remove(); //刪除原有的option
$("select[id$=ddlAreaName]").append($("<option></option>").val("-1").html("--請選擇--"));
for (var i = 0; i < data.length; i++) {
$("select[id$=ddlAreaName]").append($("<option></option>").val(data[i].AreaName).html(data[i].AreaName));
}
}
}
說明:省份數據在頁面加載時綁定,頁面提交後 省份值可以用 selectvalue取,市,區選中數據要用 Request["idname"] 獲取