gpt4 book ai didi

jquery - 在 jQuery 模板中获取索引

转载 作者:行者123 更新时间:2023-12-03 22:29:23 26 4
gpt4 key购买 nike

我正在使用 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

我应该做什么?

最佳答案

{{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 的,就像我上面所做的那样。

关于jquery - 在 jQuery 模板中获取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com