2010-11-18 12 views
7

estoy usando la plantilla plugin de jQuery y no saben cómo obtener el índice de los elementos: http://api.jquery.com/category/plugins/templates/Obtener Índice de plantilla jQuery

Aquí está mi código:

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

quiero mostrar la respuesta en el formato como el

1) answer1 siguiente, 2) respuesta2, 3) answer3

o

a) respuesta1, b) respuesta2, c) respuesta3

¿Qué debo hacer?

Respuesta

21

Hay una implícita $index (y $value) disponible dentro de la {{each}} loop, puede usar eso aquí:

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

es probable que desee añadir 1 porque es 0 basado, como si tuviera anteriormente.

Cuestiones relacionadas