Pregunta

Estoy usando el complemento de plantilla jQuery y no sé cómo obtener el índice de elementos:http://api.jquery.com/category/plugins/templates/

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

1)respuesta1, 2)respuesta2, 3)respuesta3

o

a)respuesta1, b)respuesta2, c)respuesta3

¿Qué tengo que hacer?

¿Fue útil?

Solución

Hay un implícito $index (y $value) disponible dentro del {{each}} bucle, puedes usarlo 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>

Probablemente querrás agregar 1 porque es 0-basado, como tengo arriba.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top