【发布时间】:2014-05-01 03:45:11
【问题描述】:
我有一个带有书籍列表的选择框。用户可以选择一本书并点击提交按钮以在单独的页面上查看章节。
但是,当用户更改选择框时,我希望部分页面刷新以显示用户在书中输入的过去笔记,并允许用户为该书写新笔记。我不希望在带有章节的下一页上完成对特定书籍的评论和创建笔记,因为它会使它变得混乱。
我在后端使用 Python/Bottle,在前端使用其 SimpleTemplate 引擎。
Currently, when the select box is changed, an ajax call receives a Json string containing the book information and all the notes.这个json字符串然后通过jQuery.parseJson()转换成json对象。
然后我希望能够循环遍历笔记并呈现一个包含多个单元格和行的表格。
我必须在 jQuery/js(而不是瓶/模板框架)中执行此操作吗?我假设我只想要部分刷新,而不是完整的。
我正在寻找一段代码,它可以通过 jQuery/js 从使用 ajax 检索的 json 对象中呈现具有可变行数的表。
<head>
<title>Book Notes Application - Subjects</title>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script>
$(document).ready(function(){
$('#subject_id').change(function(){
var subject_id = $(this).val();
$.ajax({
url : "subject_ajax?subject_id=" + subject_id,
success : function(data) {
alert(data)
json = jQuery.parseJSON(data);
},
error : function() {
alert("Error");
}
});
})
})
</script>
</head>
<body>
<!-- CHOOSE SUBJECT -->
<FORM action="/books" id="choose_subject" name="choose_subject" method="POST">
Choose a Subject:
<select name="subject_id" id="subject_id">
% for subject in subjects:
<option value="{{subject.id}}">{{subject.name}}</option>
% end
</select><input type="submit" name="sub" value="Choose Subject"/>
<BR />
</FORM>
【问题讨论】:
标签: javascript jquery python html ajax