8

我正在使用 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>

我想以如下格式显示答案

1)答案1, 2)答案2, 3)答案3

或者

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

我该怎么办?

4

1 回答 1

23

在循环中有一个隐式$index(and $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>

您可能想要添加1,因为它是0基于 - 的,就像我上面所说的那样。

于 2010-11-18T09:20:57.687 回答