ajax二级联动代码实例
//二级联动 $(function () { var _in_progress = false; function check_in_progress() { if (_in_progress == true) return false; return true; } function finish_in_progress() { _in_progress = false; } if (check_in_progress()) { //获取一级下拉数据,加载页面时加载数据 $.ajax({ type: "post", url: "/Resources/askCat.ashx", success: function (data) { var listarr = eval(data); if (listarr != null) selectinit(listarr, "selectCatId"); }, dataType: "json" }); finish_in_progress(); } //第一个下拉的onchange事件绑定 $("#selectCatId").change(function () { if ($(this).val() == 0) { $("#selectCatId2").hide(); } else { $("#selectCatId2").show(); select2datasouse($(this).val()); } }); }); //获取二级下拉的数据(ajax) function select2datasouse(catid) { $.ajax({ type: "post", data: { "catid": catid }, url: "/Resources/askCat.ashx", success: function (data) { var da = eval(data); if (da != null) selectinit(da, "selectCatId2"); }, dataType: "json" }); }
//加载数据 function selectinit(data, selectid) { if (data != null) { var $select1 = $("#" + selectid); $select1.empty(); $("<option/>").attr("value", "0").html("--请选择--").appendTo($select1); $.each(data, function (i, v) { $("<option/>").attr("value", v.AskCatId).html(v.Title).appendTo($select1); }); } }