JavaScript 多重自增变量解决方案
我有一个变量 ver i = 1
。
我有一张表如下;
<table>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr class="testrow">
<tr>
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
</table>
该表可能有更多行。我希望变量在单击 next
时将值增加 1,在单击 prev
时将值减少 1。这很容易做到。但我想要一些与行相关的变量。当我在第一行中单击 next
时,变量值应为 2,但当我在任何行中单击 next
或 prev
时,变量值不应更改其他行。所有其他行也应该如此。变量的最小值应为 1。
如果有人向我提供每行中间单元格中显示的变量的小提琴,将会很有帮助。请注意,在此演示中,它不应该是 ++
或 --
中间单元格中的文本或数据。
这里是我的小提琴。
I have a variable ver i = 1
.
I have a table as follows;
<table>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
<tr class="testrow">
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr class="testrow">
<tr>
<td class="prev"> </td>
<td class="data"> </td>
<td class="next"> </td>
</tr>
</table>
The table may have more rows. I want a variable to increase value by 1 when I click next
and decrease by 1 for prev
. This can be done easily. But I want some variable which is row dependent. When I clicknext
in first row, the variable value should be 2, but it should not change when I click either next
or prev
in any other row. Also this should be the case in all other rows. The minimum value of variable should be 1.
It will be helpful if someone provide me a fiddle with the variable displayed in the middle cell of each row. Note that in this demo, it should not be something to ++
or --
the text or data in the middle cell.
Here is my fiddle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 jQuery.data() 将变量存储在每一行中,并在用户点击上一个/下一个:
jsFiddle:http://jsfiddle.net/5TPCK/12/
I'd use jQuery.data() to store the variable in each row, changing it when the user clicks prev/next:
jsFiddle: http://jsfiddle.net/5TPCK/12/
http://jsfiddle.net/ThiefMaster/5TPCK/2/
顺便说一句,
< ;/tr class="testrow">
是非常错误的 - 它应该只是。
http://jsfiddle.net/ThiefMaster/5TPCK/2/
Btw,
</tr class="testrow">
is horribly wrong - it should only be</tr>
.你不能保留这些计数器的数组(如果行数事先已知并且是静态的,这将起作用)?否则,您可以使用 jquery
data()
函数将计数器附加到每个元素。
请参阅:http://api.jquery.com/jQuery.data/
Couldn't you keep an array of these counters (this would work if the number of rows is known beforehand and static)? Otherwise you could attach the counter to each
<tr>
element using the jquerydata()
function.See: http://api.jquery.com/jQuery.data/