8

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)答え1、2)答え2、3)答え3

また

a)答え1、b)答え2、c)答え3

私は何をすべきか?

4

1 に答える 1

23

loop内で使用可能な暗黙の$index(and $value)があり、ここで使用できます。{{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

于 2010-11-18T09:20:57.687 に答える