在jQuery模板中获取索引 [英] Get Index in jQuery template

查看:213
本文介绍了在jQuery模板中获取索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我该怎么办?

解决方案

$index(和$value)可用. -each/"rel =" noreferrer> {{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的,就像我上面提到的那样.

I am using the jQuery template plugin and don't know how to get the index of items: http://api.jquery.com/category/plugins/templates/

Here is my code:

<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>

I want to show the answer in the format like the following

1)answer1, 2)answer2, 3)answer3

or

a)answer1, b)answer2, c)answer3

What should I do?

解决方案

There's an implicit $index (and $value) available inside the {{each}} loop, you can use that here:

<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>

You'll probably want to add 1 because it's 0-based, like I have above.

这篇关于在jQuery模板中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆