在jQuery模板中获取索引

Bil*_*lly 8 jquery jquery-templates

我正在使用jQuery模板插件,不知道如何获取项目索引:http: //api.jquery.com/category/plugins/templates/

这是我的代码:

<script id="optionTmpl" type="text/x-jquery-tmpl">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    {{each Answers}}
        <tr>
            <th><input type="radio" name="group1" value="${this.AnswerID}" /></th>
            <td>${this.AnswerText}</td><!-- add number in this line--->
        </tr>
    {{/each}}  
    </table>
</script>
Run Code Online (Sandbox Code Playgroud)

我想以下面的格式显示答案

1)回答1,2)回答2,3)回答3

要么

a)答案1,b)答案2,c)答案3

我该怎么办?

Nic*_*ver 22

循环中有一个隐式$index(和$value)可用,你可以在这里使用:{{each}}

<script id="optionTmpl" type="text/x-jquery-tmpl">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    {{each Answers}}
        <tr>
            <th><input type="radio" name="group1" value="${this.AnswerID}" /></th>
            <td>${this.AnswerText} ${$index + 1}</td>
        </tr>
    {{/each}}  
    </table>
</script>
Run Code Online (Sandbox Code Playgroud)

你可能想要添加,1因为它是0基于我的,如上所述.