使用JSON填充jQuery的UL [英] Using JSON To Populate UL With jQuery
问题描述
这是我的JSON数据
[
{"CountyId":2,"Name":"Snohomish","StateId":1,"State":null,"Plans":null},
{"CountyId":1,"Name":"Whatcom","StateId":1,"State":null,"Plans":null}
]
我正在尝试使用此数据填充我网页上的UL.
I'm attempting to populate a UL on my web page with this data.
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()
,因为通过select.html('')
调用清除了我的UL.但是没有物品被放回UL.
I know that i'm successfully calling the loadSavedQueries()
because my UL is being cleared out with the select.html('')
call. But no items are being put back into the UL.
在明显的修复和更改不起作用之后,我发现控制器内的一个问题并没有引发错误,而是基本上返回了空的JSON对象.一旦我发现了这一点,数据就开始向下流动,而建议的更改就可以解决问题.
After the obvious fixes and changes not working, I discovered an issue within the controller that wasn't throwing an error, but was basically returning empty JSON objects. Once I caught this, the data started flowing down and the changes suggested did the trick.
推荐答案
您可以在最后设置UL的html-无需先清除它:
You can set the UL's html at the end - no need to clear it out first:
function populateSavedCounties(select, data) {
var items = [];
$.each(data, function (id, option) {
items.push('<li>' + option.Name + '</li>');
});
select.html(items.join(''));
}
这篇关于使用JSON填充jQuery的UL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!