2010-11-18 66 views
7

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

这里是我的代码:

<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

我该怎么办?

回答

21

有可用的{{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,像我有以上。