在我的页面上,我有一个包含UL的部分,每个UL都从一个字符串数组中填充
dealers = [{
title : "Brecksville",
address : "8383 Chippewa Rd.,",
locale : "Brecksville, Ohio - 44141",
phone : "(440) 740-0535",
location: {lat: 41.32134300, lng: -81.62338300}
}];
为简洁起见,我只显示字符串数组中的一个字符串......
此字符串填充到UL中,如此...
function populatDealerSummaryList(select, dealers) {
$listSelector = $("#dealersList"); //The Dealer Summary List
$.each(dealers, function (i, obj) {
$listSelector.append(
"<ul class='dealer-summary-list'>" +
"<li class='dealerName'>" + obj.title + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.address + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.locale + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.location + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.phone + "</li>" +
"</ul>");
});
}
以上......纬度和经度由obj.location ...
表示问题是...... lat long只渲染为[object,object]
我不确定如何正确传递它以使其呈现...请帮助
答案 0 :(得分:4)
您得到此结果是因为Object
方法toString()
返回此输出字符串"[object Object]"
;
您可以这样写:
...>" + JSON.stringify(obj.location) + "<...
或
...>" + obj.location.lat + ', ' + obj.location.lng + "<...
看看JSFiddle。有两种方法
答案 1 :(得分:1)
obj.location is an object that contains two keys inside.
您将整个<li>
传递给object
元素。
你应该只是:
"<li class='dealer-summary-listItem'>" + obj.location.lat + " " + obj.location.lng + "</li>"