你好,我有这个输出使用php
{“日期”:“2019-02-10”,“诉状”:1},{“日期”:“2019-02-12”,“恳求”:1},{“日期”:“2019-02-14”,“答辩”:1}
我可以通过Jquery代码轻松地访问它。
// AJAX Request to get the data by UserID and Week 1
$.get('/dashboard/performance?name=' + document.getElementById('agents').value + '&week=0', function(data){
$.each(data,function(i,value){
var tr =$("<tr/>");
tr.append($("<th/>",{
text : value.date
})).append($("<th/>",{
text : value.pleads
}))
$('#tableData-performance-week1').append(tr);
})
});但是,由于我必须做一些修改才能从db返回多个数据,那么php返回的是什么呢?
{“日期”:{“日期”:“2019-02-02”、"aleads":1}、“经理”:{“日期”:“2019-02-01”、“m铅”:1}、“人物线索”:{“日期”:“2019-02-02”、“恳求”:2}、{“日期”:“2019-02-03”、“答辩”:1}、{“日期”:“2019-02-04”、“答辩书”:1},{“日期”:“2019-02-05”、“恳求”:1}、{“日期”:“2019-02-06”、“恳求”:1}
我如何通过JQuery访问它们?因为我试过了,但我无法用正常的方式访问它们--非常感谢
发布于 2019-03-20 18:27:48
jQuery.each也将处理对象,在这种情况下,第一个参数将是属性名称,第二个参数将是属性值。
现在您必须迭代数组(内部数组,属性值),为此使用Array#forEach方法,并在循环中检查是否定义了plead属性,然后创建并追加tr。要检查已定义或未定义的属性,可以使用 operator。
// iterate over all values
$.each(data, function(k, arr) {
// iterate over all objects within array
arr.forEach(function(value) {
// check pleads property defined if defined
// do the rest as you did early
if ('pleads' in value) {
var tr = $("<tr/>");
tr.append($("<th/>", {
text: value.date
})).append($("<th/>", {
text: value.pleads
}))
$('#tableData-performance-week1').append(tr);
}
});
})
var data = {
"adminleads": [{
"date": "2019-02-02",
"aleads": 1
}],
"managerleads": [{
"date": "2019-02-01",
"mleads": 1
}],
"personalleads": [{
"date": "2019-02-02",
"pleads": 2
}, {
"date": "2019-02-03",
"pleads": 1
}, {
"date": "2019-02-04",
"pleads": 1
}, {
"date": "2019-02-05",
"pleads": 1
}, {
"date": "2019-02-06",
"pleads": 1
}]
}
$.each(data, function(k, arr) {
arr.forEach(function(value) {
if ('pleads' in value) {
var tr = $("<tr/>");
tr.append($("<th/>", {
text: value.date
})).append($("<th/>", {
text: value.pleads
}))
$('#tableData-performance-week1').append(tr);
}
});
})<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tableData-performance-week1"></table>
https://stackoverflow.com/questions/55267757
复制相似问题