gpt4 book ai didi

javascript - jquery 选择与此一起

转载 作者:行者123 更新时间:2023-11-30 13:06:00 24 4
gpt4 key购买 nike

我有这样的 HTML

<table>
<tr>
<td>
<select>
<option>12AM</option>
<option>11PM</option>
</select>
</td>
<td>
<select>
<option>12AM</option>
<option>11PM</option>
</select>
</td>
</tr>
</table>

我想为每个 tr 中的 select 标签分配一个 id 属性。下面的jquery好像不行

function addRow()
{
var rowCount = 0;
$('#SHourDivTable tr').each(function() {
if(rowCount != 0)
{
console.log($(this).html());
$(this).add("select:eq(0)").attr("id", rowCount);
$(this).add("select:eq(1)").attr("id", rowCount);
}
rowCount = rowCount + 1;
});
}

当我运行它时,id 属性被分配给 tr 而不是 select 标签。

最佳答案

ID 必须是唯一的。它们也不应以数字开头。你应该使用 data 属性来做到这一点。

我还使用了 table 而不是 #SHourDivTable,因为我在您的标记中看不到具有该 ID 的表格。

最后,您需要find 而不是 add 来完成您在这里所做的事情。

试试这个:

function addRow()
{
var rowCount = 0;
$('table tr').each(function() {
if(rowCount != 0)
{
$(this).find("select:eq(0)").data("rowNumber", rowCount);
$(this).find("select:eq(1)").data("rowNumber", rowCount);
}
rowCount = rowCount + 1;
});
}

关于javascript - jquery 选择与此一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15669858/

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