|
发表于 2014-7-9 08:30:22
|
显示全部楼层
View中
@Html.DropDownListFor(r => r.md.Province, new SelectListItem[] { new SelectListItem() { Text = "请选择" } }, new { @id = "drpProvince" })
@Html.DropDownListFor(r => r.md.City, new SelectListItem[] { new SelectListItem() { Text = "请选择" } }, new { @id = "drpCity" })
@Html.DropDownListFor(r => r.md.District, new SelectListItem[] { new SelectListItem() { Text = "请选择" } }, new { @id = "drpDistrict" })
<script type="text/javascript">
var p = '@(Model.md == null ? "0" : Model.md.Province)';
var c = '@(Model.md == null ? "0" : Model.md.City)';
var d = '@(Model.md == null ? "0" : Model.md.District)';
function changer1(obj, i) {
obj.get(0).options.length = 0; //清空
$("<option></option>").text("请选择").appendTo(obj);
$.ajax({
type: 'GET',
url: '/home/ajax/getcitys/' + i,
dataType: 'json',
success: function (data) {
var _data = data.Data.d;
$.each(_data, function (t, item) {
var temp = item.split("_");
$("<option></option>").val(temp[0]).text(temp[1]).appendTo(obj);
});
if (i == p) {
$("#drpCity").val(c);
}
else if (i == c) {
$("#drpDistrict ").val(d);
}
}
});
}
$(function () {
//初始化
$.ajax({
type: 'get',
dataType: 'json',
url: '/home/ajax/getcitys/1',
data: { pid: 0 },
success: function (data) {
var _data = data.Data.d;
$.each(_data, function (j, item) {
var temp = item.split("_");
$("<option></option>").val(temp[0]).text(temp[1]).appendTo($("#drpProvince"));
});
$("#drpProvince").change(function () {
changer1($("#drpCity"), $(this).val());
if (p == "0") {
$("#drpDistrict").get(0).selectedIndex = 0;
}
});
$("#drpCity").change(function () {
changer1($("#drpDistrict"), $(this).val());
});
if (p != "0") {
$("#drpProvince ").val(p);
changer1($("#drpCity"), p);
if (c != "0") {
changer1($("#drpDistrict"), c);
}
}
}
});
$("#btnSava").click(function () {
if ($("#txtName").val().length < 1) {
alert("收件人不能为空");
return false;
}
if ($("#txtMobile").val().length < 1) {
alert("手机号不能为空");
$("#lfPassword").focus();
return false;
}
return true;
});
});
</script>
控制器中
public ActionResult Ajax(string method, string param, FlightContainer data)
{
object resultData = null;
string resultKey = "";
method = (method + "").ToLower();
switch (method)
{
case "getcitys":
{
int depth;
var list = _webCacheService.getSystemAddresss(param + "", out depth);
resultData = new { k = (depth == 1 ? "p" : (depth == 2 ? "c" : "d")), n = depth < 3, d = list };
break;
}
default:
break;
}
return Json(new BasicJsonMessage()
{
Result = resultData != null,
Key = resultKey,
Data = resultData
}, JsonRequestBehavior.AllowGet);
} |
|