Jquery绑定Select下拉菜单
需要返回JSon数据集。
ASPX:
$.ajax({
type: "POST",
contentType: "application/json;utf-8",
data: "{strclassid:'" + strclassid + "'}",
url: "/AutoService.asmx/Show",
success: function (msg) {
var myObject = eval('(' + msg.d + ')');
for (var i = 0; i < myObject.length; i++) {
$("#selectabc").append(("<option value='" + myObject[i].Id + "'>" + myObject[i].Names + "</option>"));
}
Cs文件中可用
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(USERS)
序列化
直接生成JSON文件更方便:
StringBuilder sbGoodsName = new StringBuilder(); sbGoodsName.Append("[{\"GoodsName\":\"请选择\",\"GoodsID\":\"-1\"}"); DbDataReader dr = db.ExecuteReader(cmd); while (dr.Read()) { sbGoodsName.Append(",{"); sbGoodsName.Append("\"GoodsName\":" + "\"" + dr["names"].ToString() + "\","); sbGoodsName.Append("\"GoodsID\":" + "\"" + dr["id"].ToString() + "\""); sbGoodsName.Append("}"); } dr.Close(); sbGoodsName.Append("]"); return sbGoodsName.ToString();