我有如下ajax调用的数据:
[ { "id": "1", "name": "Jim", "age": "39", "address": "12 High Street, London", "hasCar": "1", "speaksForeignLanguage": "1", "canWorkNights": "1", "isStudent": "1" }, { "id": "2", "name": "Fred", "age": "29", "address": "13 High Street, London", "hasCar": "1", "speaksForeignLanguage": "1", "canWorkNights": "1", "isStudent": "0" }, { "id": "3", "name": "Bill", "age": "19", "address": "14 High Street, London", "hasCar": "1", "speaksForeignLanguage": "1", "canWorkNights": "0", "isStudent": "0" }, { "id": "4", "name": "Tom", "age": "39", "address": "15 High Street, London", "hasCar": "1", "speaksForeignLanguage": "0", "canWorkNights": "0", "isStudent": "0" }, { "id": "5", "name": "Cathy", "age": "29", "address": "16 High Street, London", "hasCar": "1", "speaksForeignLanguage": "0", "canWorkNights": "0", "isStudent": "1" }, { "id": "6", "name": "Petra", "age": "19", "address": "17 High Street, London", "hasCar": "1", "speaksForeignLanguage": "0", "canWorkNights": "1", "isStudent": "0" }, { "id": "7", "name": "Heide", "age": "39", "address": "18 High Street, London", "hasCar": "1", "speaksForeignLanguage": "1", "canWorkNights": "0", "isStudent": "0" }, { "id": "8", "name": "William", "age": "29", "address": "19 High Street, London", "hasCar": "1", "speaksForeignLanguage": "1", "canWorkNights": "0", "isStudent": "1" }, { "id": "9", "name": "Ted", "age": "19", "address": "20 High Street, London", "hasCar": "0", "speaksForeignLanguage": "0", "canWorkNights": "0", "isStudent": "1" }, { "id": "10", "name": "Mike", "age": "19", "address": "21 High Street, London", "hasCar": "1", "speaksForeignLanguage": "0", "canWorkNights": "0", "isStudent": "1" }, { "id": "11", "name": "Jo", "age": "19", "address": "22 High Street, London", "hasCar": "0", "speaksForeignLanguage": "1", "canWorkNights": "0", "isStudent": "1" } ]我试图用这些数据绘制一个div元素,但是我只想要特定的数据信息来描述我想要绘制的内容。
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
$.each(this, function(k , v) {
console.log(v.age);
tbl_body += "<spam>"+v.age+"</spam>";
})
})
return tbl_body;
}但是它说v.age在未定义的:
这里是我的ajax调用:
function updateEmployees(opts){
$.ajax({
type: "POST",
url: "submit.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('#listings').html(makeTable(records));
}
});
}在这个例子中,我怎么才能得到这个年龄呢?
发布于 2014-07-16 18:57:59
您使用的是两个函数。为什么会这样呢?在对象属性中进行迭代。在这种情况下你不需要那个..。试试这个:
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
console.log(this.age);
tbl_body += "<span>"+this.age+"</span>";
})
return tbl_body;
}还请注意,我将<spam>更改为<span>。你写错了,我不知道你想做什么,但这不能做任何桌子。问问你能不能帮上忙。
https://stackoverflow.com/questions/24788514
复制相似问题