建议我使用JQuery模板插件,我想知道如何在脚本中创建一个计数器' name_tmpl'它只会遍历并显示名称4次而不是显示JSON文件中的每一个名称。
$.ajax({
type: 'GET',
dataType: "json",
url: "url.com",
success: showAllNames
});
function showAllNames(responseData) {
$("#name_tmpl").tmpl(responseData.games).appendTo("#names-holder");
}
<script id="name_tmpl" type="text/x-jQuery-tmpl">
<p>${name}</p>
</script>
答案 0 :(得分:0)
您可以使用模板中的每个函数,如下所示:
<script id="name_tmpl" type="text/x-jQuery-tmpl">
{{each(i, name) names}}
{{if i<4}}
<p>${name}</p>
{{/if}}
{{/each}}
</script>
并将其命名为:
function showAllNames(responseData) {
$("#name_tmpl").tmpl({"names":responseData.names}).appendTo("#names-holder");
}