【发布时间】:2014-01-07 20:27:32
【问题描述】:
我有一个大约四列的 HTML 表格。我正在使用 Ajax 查询数据,该 Ajax 使用 Django 从 SQL 表中提取信息。对于最后一列,我需要一个下拉菜单。
我从 Ajax 获取的 JSON 信息示例如下:
[[1, "abc", 1, ["[2013-09-30]", "[2013-12-02]"]]
[[2, "def", 1, ["[2013-09-30]"]]
它基本上显示 ID、一些字符串、频率和日期。我有大约 25 个不同的 ID。每个 ID 中的前三个数据点都具有相同的信息,但唯一不同的是我的日期。这就是为什么我需要一个下拉菜单来为每个 ID 选择日期。
我的问题是我的表格中的下拉菜单无法正常工作。它显示第一行中的所有日期,而其余行中的所有其他菜单都是空白的。我知道我的循环中有一个问题,我将下拉菜单附加到表格中,但我无法弄清楚发生了什么。任何帮助将不胜感激。
HTML
<table id = "templates" border = "1">
<thead>
<tr>
<th> Template ID </th>
<th> Template Name </th>
<th> Freq Multiplier </th>
<th> As of Date </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JQUERY
<script type="text/javascript">
$(document).ready(function fill_table() {
$.ajax({
type:"GET",
url: "/Tplots/ajax_temp/",
dataType:'json',
success: function(response) {
for(var i = 0; i< response.length; i++)
{
var item = response[i];
for (var j = 0; j<10;j++) {
var tempID = item[j];
j = 1;
var tempName = item[j];
j =2;
var freqM = item[j];
j = 3;
var as_of = item[j];
j=4;
$('#templates tbody').append("<tr id = i><td>" + tempID + "</td><td>" + tempName + "</td><td>" + freqM + "</td><td><select name='dropdown' id = 'dropdown'></select></td></tr>");
for (var k = 0; k<as_of.length; k++)
{
var asofdate = as_of[k];
$('#dropdown').append($("<option>"+asofdate+"</option>"));
}
}
}
}
})
})
</script>
【问题讨论】:
标签: javascript jquery html sql django