【问题标题】:Get Index in jQuery template在 jQuery 模板中获取索引
【发布时间】:2011-05-11 21:54:36
【问题描述】:

我正在使用 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)answer1、2)answer2、3)answer3

a)answer1, b)answer2, c)answer3

我该怎么办?

【问题讨论】:

    标签: jquery jquery-templates


    【解决方案1】:

    {{each}} loop 中有一个隐含的$index(和$value),你可以在这里使用它:

    <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 的,就像我上面所说的那样。

    【讨论】:

      猜你喜欢
      • 2011-09-09
      • 1970-01-01
      • 2012-05-25
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2016-01-03
      相关资源
      最近更新 更多