[英]jQuery $.each() returns only the first element from a Json object
我想通過循環通過Json對象(結果)的$ .each()函數動態填充#city div的內容。 如何從對象中獲取所有元素並將其放入#city div中?
在這種情況下創建和替換html元素的最佳方法是什么? (從每個功能出發)
我的代碼:
<div id="city"></div>
$.ajax({
url: "/Home/GetCity",
type: "GET",
data: { county: County}
})
.done(function (result) {
$.each(result, function (index, value) {
console.log(this.CityName); //here it returns all the elements
$('#city').html("<h4>" + this.CityName+ "</h4>"); //here it return only the first one
});
});
您正在覆蓋所有值,因為您正在使用“ html” jquery函數。 我建議使用附加:
$('#city').empty().append("<h4>" + this.CityName+ "</h4>");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.