我想通过运行循环来创建几行,如下面的代码所示:
<table>
<% for (var i = 0; i <= 2; i++)
{ %>
<tr id="Row" +"i"> // i want to give unique row ID based on my "i" variable
<td><%:Html.TextBoxFor(m=>m.ChildData[i].ChildName) %></td>
</tr>
<%} %>
</table>
在结果表中,我希望每一行都有一个唯一的ID:
<tr id="Row1">,<tr id="Row2">,<tr id="Row3">, etc.
我该怎么做?
答案 0 :(得分:1)
<table>
<% for (var i = 0; i <= 2; i++) { %>
<tr id="Row<%= i %>">
<td>
<%= Html.TextBoxFor(m => m.ChildData[i].ChildName) %>
</td>
</tr>
<% } %>
</table>
但是如果你需要那些id用javascript来操作行,那么你真的不需要分配任何id。例如,jQuery为您提供了允许您传递选择器的当前索引的函数。例如:
$('table tr').each(function(index, row) {
// build the id, the same way as if you were building it on the server
var id = 'Row' + (index + 1);
// get the corresponding textbox that's inside this row
var textbox = $('input[type="text"]', row);
...
});