[英]Onclick change or insert text in dynamic table cell span
I have a dynamic table, with multiple tr and td and buttons in it. 我有一个动态表,其中有多个tr和td以及按钮。 I want to insert a counting number infront of td content, every time the (up)-button is clicked.
每次单击(向上)按钮时,我想在td内容的前面插入一个计数数字。 Something like this: [3 x ABC].
像这样:[3 x ABC]。
My demo does not work, but my current version; 我的演示不起作用,但是我当前的版本; what ever button I click on, the number appears and increase in all of the rows at once.
无论我单击什么按钮,数字都会出现,并在所有行中一次增加。 I want the buttons to function in respektiv rows.
我希望按钮在respektiv行中起作用。 How to manage that in jquery?
如何在jQuery中管理? Please.
请。
$(function(){
var count=1;
$('.up').click(function(){
count++
$('.times').text(count+' x ');
})
});
This line is the problem: 这行是问题所在:
$('.times').text(count+' x ');
It selects every occurrance of .times
and sets a text to all of them. 它选择的每一个 occurrance
.times
并设置文本他们。
In your case, you want the one on the same row. 对于您的情况,您希望将其放在同一行。 So from the clicked element (
this
), go up and find the closest tr
in the chain of ancestors. 因此,从被单击的元素(
this
)中,向上找到祖先链中最接近的tr
。 Then, within that row, find a .times
again to set the text in: 然后,在该行中,再次找到
.times
以将文本设置为:
$(this).closest('tr').find('.times').text(count+' x ');
If you want to have a counter per row too, you could store the counter information in a data attribute of the element. 如果也希望每行都有一个计数器,则可以将计数器信息存储在元素的data属性中。 Another, arguably better, solution is to create closures that hold the variable.
另一个可能更好的解决方案是创建保存变量的闭包。 That latter solution I've implemented in...
我在...中实现的后一种解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.