如何将项目附加到现有handelbar模板中的列表中
how to append an item to a list in an existing handelbar template?
我正试图在现有的手把模板中添加一个列表。下面的代码允许我添加注释,但为了使新注释与现有注释一起出现在列表中,我需要刷新。我想知道的是如何将新的注释附加到模板中现有的注释列表中。我已经包含了整个模板,但我试图添加新评论的地方是id为bodyOfComments的ul。谢谢你的帮助。
var comment_post = function() {
// console.log($(this).attr("data-id"))
$.ajax({
url: 'http://localhost:3000/comments',
type: 'POST',
data: {comment: {
body: $('#content').find('input[name="createComment"]').val(),
user_id: 1,
image_set_id: $(this).attr("data-id")}
}
}).done(function(response) {
console.log(response);
var template = Handlebars.compile($('#imageSetTemplate').html());
$('#bodyOfComments').append(template({
comment: response
}));
});
};
$(document).ready(function () {
$('#content').on('click', '#submitComment', comment_post)
});
下面的代码是html文件中的手柄模板
<script id="imageSetTemplate" type="text/x-handlebars-template">
<h1>{{image_set.voting_criteria}}</h1>
<div class="continer">
<div class="row">
{{#each image_set.images}}
<div class = "col-xs-4"><img src={{this.image_url}}></div>
{{/each}}
</div>
</div>
<ul id="bodyOfComments">
{{#each image_set.comments}}
<li> {{this.body}} </li>
{{/each}}
</ul>
<button type="submit" id="submitComment" data-id={{image_set.id}}>Create Comment</button>
<input name="createComment" id="commentBody">
</script>
将注释模板移出为一个单独的模板。
<script id="commentsTemplate" type="text/x-handlebars-template">
{{#comments}}
<li> {{.}} </li>
{{/comments}}
</script>
然后您可以附加如下。
var template = Handlebars.compile($('#commentsTemplate').html());
$('#bodyOfComments').append(template({
comments: response
}));
相关文章:
- 要求输入在数据列表中
- 从javascript创建一个列表
- 如何使用jQuery选择下拉列表的值
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- Javascript按钮下拉列表
- 如何使用json将对象列表从java转换为javascript
- 区分JSON中的矩阵和列表列表
- jQuery表单添加不适用于下拉列表
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 接受不在列表中的值-引导组合框
- 禁用jQuery中的下拉列表
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 如何在javascript中迭代数字列表
- 如何在按钮中显示下拉列表中选定的元素
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- 如何将项目附加到现有handelbar模板中的列表中