我有以下JavaScript来填充下拉列表。当我单步进入populateResourceList
时,jsonList
的值会显示在代码下方。但是,此方法在for
语句上失败,错误为 jsonList.Table未定义
我做错了什么?
function populateResourceList(jsonList) {
var listItems = "";
for (var i = 0; i < jsonList.Table.length; i++) {
listItems += "<option value='" + jsonList.Table[i].id + "'>" + jsonList.Table[i].name + "</option>";
}
$("#<%=resourceList.ClientID %>").html(listItems);
};
$(function () {
var sessionId = 554;
var x = PageMethods.GetFreeResources(sessionId, function (result, response, context) {
populateResourceList(result);
});
});
编辑: jsonList看起来像这样:
'{"Table": [ {"id": "1", "name": "Billy Bester"}, {"id": "2", "name": "Marlene Smith"}, {"id": "3", "name": "John Mills"}, {"id": "4", "name": "Brady Kelly"}, {"id": "5", "name": "Andrew Peel"}, {"id": "6", "name": "Colin Clifford"}, {"id": "7", "name": "Kerri Steele"}, ]}'
答案 0 :(得分:1)
你确定它是文字而不是字符串吗?
试试这个并告诉我。评估是邪恶的,但它会告诉我们这是否是我们想要的方向。
function populateResourceList(jsonList) {
eval("var list=" + jsonList + ";");
var listItems = "";
for (var i = 0; i < list.Table.length; i++) {
listItems += "<option value='" + list.Table[i].id + "'>" + list.Table[i].name + "</option>";
}
$("#<%=resourceList.ClientID %>").html(listItems);
};