【发布时间】:2017-08-31 16:08:57
【问题描述】:
我在 index.html 中有以下代码,它获取字典列表并将键和值打印到表中
$(function() {
$('a#search').bind('click', function() {
$.getJSON('/_search', {
a: $('input[name="a"]').val()
}, function(data) {
var tableData = '<table>'
$.each(data.result, function(key, value){
tableData += '<tr><td>' + ' ' + key + ' ' + '</td>';
alert(key)
$.each(value, function(val){
alert(value[val])
tableData += '<td>' + value[val] + '</td>';
});
tableData += '</tr>';
});
tableData += '</table>';
$('#table').html(tableData);
});
它抓取的是来自 search.py 的字典列表
result = defaultdict(list)
return jsonify(result=result)
结果包含以下内容
defaultdict(<class 'list'>, {'Developer': ['Office Koukan', 'Jorudan', 'Beam Software'], 'Publisher': ['Shouei', 'VAP', 'Hi Tech Expressions'], 'ReleaseDate': ['March 18, 1994', 'November 18, 1994', 'October 1, 1993'], 'Title': ['Idea no Hi', 'Pachinko Hi Hisshouhou', 'hunThe Hunt for Red October']})
但是我的输出如下
Developer Publisher ReleaseDate Title
Office Koukan Jorudan Beam Software
Shouei VAP Hi Tech Expressions
March 18, 1994 November 18, 1994 October 1, 1993
Idea no Hi Pachinko Hi Hisshouhou hunThe Hunt for Red October
什么时候应该输出
Developer Publisher ReleaseDate Title
Office Shouei ... ...
Koukan VAP ... ...
Jorudan ... ... ...
Beam Software ... ... ...
知道我可能做错了什么吗?任何帮助将不胜感激?
【问题讨论】:
标签: python html dictionary html-table