gpt4 book ai didi

javascript - 如何使用 jQuery 获取表中一行的每个 id?

转载 作者:行者123 更新时间:2023-11-28 11:09:31 25 4
gpt4 key购买 nike

为什么每次迭代都会给出第一个 id?或者更好的是,我如何获取每个 id?

$('#foo').on('click', function () {
var rows = $('#mytable tbody tr.selected');
for (var i = 0; rows[i]; i++) {
console.log('rows.attr("id")' + rows.attr('id'));
}
});

当选择 2 行时,将打印“17”两次。我可以看到第二行是“18”,所以出了问题。

最佳答案

我建议您使用.each()

the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

var rows = $('#mytable tbody tr.selected');
rows.each(function(){
console.log(this.id); //Here this refers current row
})

或者

var rows = $('#mytable tbody tr.selected');
//Here you need to add break condition
for (var i = 0; i < rows.length; i++) {
console.log('rows.attr("id")' + rows[i].id); //access row using index
}

关于javascript - 如何使用 jQuery 获取表中一行的每个 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28829909/

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