并在把手模板中呈现?"/>
如何从ajax成功传递数据并在把手模板中呈现?
我在.hbs文件中拥有这个
<div class="col-12 col-md-3 order-md-1">
<div id="notes">
{{#each notes}}
{{>note}}
{{/each}}
</div>
</div>
这是我的jquery:
$.ajax({
url: '/search/notes',
method: 'GET',
data: {
search_string: 'server'
},
success: function(response) {
var source = $('#notes').html()
var template = Handlebarspile(source)
var context = {
notes : response.notes,
}
var el_html = template(context);
console.log(el_html)
$('#notes').html(el_html)
}
})
<script src=".js/2.0.0/handlebars.js"></script>
我无法在notes
上获得id="notes"
。结果为空白div。任何帮助吗?
可能与您的Ajax响应有关。下面的示例在没有Ajax的情况下工作。
setTimeout(function() {
var source = $('#notes').html()
var template = Handlebarspile(source)
var context = {
notes : ["test", "test2"],
}
var el_html = template(context);
console.log(el_html);
$('#notes').html(el_html);
}, 2000);
<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<div class="col-12 col-md-3 order-md-1">
<div id="notes">
{{#each notes}}
{{this}}
{{/each}}
</div>
</div>
更多推荐
如何从ajax成功传递数据并在把手模板中呈现?
发布评论