这是我的
JSON数据
[
{"CountyId":2,"Name":"Snohomish","StateId":1,"State":null,"Plans":null},{"CountyId":1,"Name":"Whatcom","Plans":null}
]
我正在尝试使用此数据在我的网页上填充UL.
function loadSavedCounties() {
$.post("/Plans/GetPlanCounties/",{ planId: $("#PlanId").val() },function (data) {
populateSavedCounties($("#SavedCounties"),data);
}
);
}
function populateSavedCounties(select,data) {
select.html('');
var items = [];
$.each(data,function (id,option) {
items.push('<li>' + option.Name + '</li>');
});
select.append(items.join(''));
}
我知道我已成功调用loadSavedQueries(),因为我的UL正在通过select.html(”)调用清除.但没有任何物品被放回UL.
更新中…
在明显的修复和更改不起作用之后,我发现控制器中没有抛出错误的问题,但基本上是返回空的JSON对象.一旦我发现了这一点,数据开始流下来,并且建议的更改就可以了.
解决方法
您可以在最后设置UL的html – 无需先清除它:
function populateSavedCounties(select,data) {
var items = [];
$.each(data,option) {
items.push('<li>' + option.Name + '</li>');
});
select.html(items.join(''));
}