日期:2014-05-18 浏览次数:21142 次
[AjaxMethod()] public string getAreaTypeList() { string getAreaStr = "["; DataTable dtAreaType = new VTB_AreaTypeBLL().GetData(); if (dtAreaType.Rows.Count > 0) { foreach (DataRow drAreaType in dtAreaType.Rows) { getAreaStr += "{\"id\": \"" + drAreaType["ID"].ToString() + "\",\"name\": \"" + drAreaType["AreaTypeName"].ToString() + "\"},"; } getAreaStr = getAreaStr.TrimEnd(',') + "]"; } else { getAreaStr = ""; } return getAreaStr; } <head> <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript"> function getAreaTypeList() { var obj = XXX.Default.getAreaTypeList(); return obj.value; } function loadSelect(jsonObj) { if (jsonObj == "") { return '<option title="-1" selected="selected" value="请选择">请选择</option>'; } jsonObj = eval("(" + jsonObj + ")"); var jsonStr = ""; $(jsonObj).each(function (key, value) { jsonStr += '<option value="' + this.id + '" >' + this.name + '</option>'; }); return jsonStr; } $(function(){ var areaType= loadSelect(getAreaTypeList()); $("#area").append(areaType); }); </script> </head> <body> <form runat="server" id="form1"> <select id="area"></select> </form> </body>
------解决方案--------------------
下拉框级联demo
------解决方案--------------------
这个得自己写,大概这样
for(var i=0;i<array .length;i++){
$("#txtaddress").append("<option vlue=\""+array[i]+"\">"+array[i]+"</ooption>");
}
想让哪项被选中,就在循环里面套if语句判断,option加个属性 select=“selected”