我的页面中有以下jQuery代码:
$('#submit').click(function() {
var $table = $('#search_body');
$.getJSON("/search_issues/" + $("#folders").val(), function(data) {
$.each(data, function(index, dict) {
alert ('each index ' + index);
$.each(obj, function(key, value){
alert('each key ' + key + ':' + value);
$table.innerHTML="<tr><td>" + key + ':' + value + "</td></tr>";
});
});
});
});
在HTML标记中,我定义了一个表,并将表的主体定义为
<tbody id="search_body">
</tbody>
我在代码中放了两个警告语句,事实证明,第一个警报只弹出一次,告诉我索引是0。
但是,如果我将浏览器指向为getJSON调用生成的url,我会得到一个包含9个对象的数组。
这让我相信我在内部(或可能是外部)循环中的语法是不正确的。所以,我去了一个在线lint表单,它说我没有语法错误,所以它看起来像一个逻辑错误。
BTW,$('#folders')确实有一个有效值(它是一个下拉列表并且已被选中)
由于
当我指向浏览器时返回的对象是:
[
{
planning: 0,
id: "0e1a9dba-8bfe-4316-8923-b76f76da3171",
rank: 0,
title: "Test Issue 4"
},
{
planning: 0,
id: "16ef48ab-1257-4fe4-a4ea-bb2a4b2757f6",
rank: 0,
title: "Test Issue 3"
},
{
planning: 0,
id: "4086f816-57dd-49e7-91a4-ef2ac9573555",
rank: 0,
title: "Test Issue 1"
},
{
planning: 0,
id: "45c598f0-fd6a-48d6-9822-1ca8b0a5af4b",
rank: 0,
title: "Test Issue 2"
},
{
planning: 0,
id: "50a62544-3350-4c78-a9eb-9d79a37846ea",
rank: 0,
title: "Test Issue 4"
},
{
planning: 0,
id: "ae7fa839-1161-4d54-92da-662e6aa35936",
rank: 0,
title: "Test Issue 6"
},
{
planning: 0,
id: "c4016338-a766-46ba-a651-3879a844f141",
rank: 0,
title: "Test creating an issue"
},
{
planning: 0,
id: "d9f38b4f-d4ef-4abf-ab30-d511c0082df1",
rank: 0,
title: "Test Issue 5"
}
答案 0 :(得分:2)
我正在改变你的代码,假设这是所需的输出:
$('#submit').click(function() {
var $table = $('#search_body');
$.getJSON("/search_issues/" + $("#folders").val(), function(data) {
$.each(data, function(index, dict) {
var $tr = $("<tr></tr>").appendTo($table);
$.each(dict, function(key, value){
$tr.append("<td>" + key + ': ' + value + "</td>");
});
});
});
});
答案 1 :(得分:1)
obj
似乎没有定义?尝试替换
$.each(dict, function(key, value){
的
$.each(obj, function(key, value){
同时替换
$table[0].innerHTML += "<tr><td>" + key + ':' + value + "</td></tr>";
代表
$table.innerHTML="<tr><td>" + key + ':' + value + "</td></tr>";
连接innerHTML
的{{1}}; $table
$table[0]
元素具有属性DOM
; .innerHTML
是jQuery对象没有方法$table
.innerHTML
var data = [
{
planning: 0,
id: "0e1a9dba-8bfe-4316-8923-b76f76da3171",
rank: 0,
title: "Test Issue 4"
},
{
planning: 0,
id: "16ef48ab-1257-4fe4-a4ea-bb2a4b2757f6",
rank: 0,
title: "Test Issue 3"
},
{
planning: 0,
id: "4086f816-57dd-49e7-91a4-ef2ac9573555",
rank: 0,
title: "Test Issue 1"
},
{
planning: 0,
id: "45c598f0-fd6a-48d6-9822-1ca8b0a5af4b",
rank: 0,
title: "Test Issue 2"
},
{
planning: 0,
id: "50a62544-3350-4c78-a9eb-9d79a37846ea",
rank: 0,
title: "Test Issue 4"
},
{
planning: 0,
id: "ae7fa839-1161-4d54-92da-662e6aa35936",
rank: 0,
title: "Test Issue 6"
},
{
planning: 0,
id: "c4016338-a766-46ba-a651-3879a844f141",
rank: 0,
title: "Test creating an issue"
},
{
planning: 0,
id: "d9f38b4f-d4ef-4abf-ab30-d511c0082df1",
rank: 0,
title: "Test Issue 5"
}
];
var $table = $('#search_body');
// $.getJSON("/search_issues/" + $("#folders").val(), function(data) {
$.each(data, function(index, dict) {
alert ('each index ' + index);
$.each(dict, function(key, value){
alert('each key ' + key + ':' + value);
$table[0].innerHTML += "<tr><td>" + key + ':' + value + "</td></tr>";
});
});
// });