2010-11-18 6 views
7

J'utilise le plugin modèle jQuery et ne savent pas comment obtenir l'index des articles: http://api.jquery.com/category/plugins/templates/Get Index dans le modèle jQuery

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

Je veux montrer la réponse dans le format comme le

suivant

1) answer1, 2) Réponse2, 3) Réponse3

ou

a) réponse1, b) réponse2, c) réponse3

Que dois-je faire?

Répondre

21

Il y a un $index implicite (et $value) disponible à l'intérieur du {{each}} loop, vous pouvez l'utiliser ici:

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

Vous voulez probablement ajouter 1 parce qu'il est 0 à base, comme je l'ai ci-dessus.