【发布时间】:2012-10-05 02:35:27
【问题描述】:
我正在使用 jQuery Mobile 通过 AJAX 发布到服务器。 响应以 JSON 形式接收,我尝试使用 Mustache 模板显示该响应。 一切正常,但 jQuery 无法正确呈现,例如它不会将类、样式等添加到呈现的模板中。
加载模板并发布:
$('#continueToHome').click(function() { // called on button click
// call ajax, send no params, receive json
$.post("http://localhost:3000/app_login.json", "", function(result){
// load template
var template = $('#personTpl').html();
// render data into template
var html = Mustache.to_html(template, result);
// append to page
$('#sampleArea').html(html);
});
// display page
$.mobile.changePage( "#page-homepage", { transition: "slide"} );
});`
html:
<script id="personTpl" type="text/template">
<h1>{{first_name}} {{last_name}}</h1>
Email: {{email}} <br>
<h3>Smifrs</h3>
<ul data-role=listview>
{{#usmifrs}}<li><a href=#>{{name}}</a></li>
{{/usmifrs}}</ul>
</script>
<div id="sampleArea"></div>
问题是它不能正确显示。 当我有一个带有 'data-role=listview' 的常规 'ul' 元素时,jQuery 会为 'li' 标签注入 class='ui-listview'、div 和样式,但是当我使用我的模板时它不会发生。
我想我应该以某种方式重新加载页面,但我找不到方法。
感谢您的帮助。
【问题讨论】:
-
你能告诉我这个 URL
http://localhost:3000/app_login.json的返回值是多少吗?你能用传递给 mustache 模板的 JSON 更新问题吗
标签: ajax jquery-mobile mustache