gpt4 book ai didi

javascript - ID 中具有跳过序列的动态控件

转载 作者:行者123 更新时间:2023-11-27 23:46:07 26 4
gpt4 key购买 nike

我正在使用 JavaScript 向 HTML 页面动态添加一些控件。可以说我有这个

ID   Name          Type                   Is Valid
(TextBox) (Select/DropDown) (CheckBox)
--- ------------ --------------------- ----------
1 txtName_1 ddlType_1 chkValid_1
2 txtName_2 ddlType_2 chkValid_2
3 txtName_3 ddlType_3 chkValid_3
4 txtName_4 ddlType_4 chkValid_4

现在我删除一些行(例如 ID 为 3 的行)并添加一些新行。新控件命名为(“txtName_”+“最后一行 ID”+ 1)。所以它可能会变成

ID   Name          Type                   Is Valid
(TextBox) (Select/DropDown) (CheckBox)
--- ------------ --------------------- ----------
1 txtName_1 ddlType_1 chkValid_1
2 txtName_2 ddlType_2 chkValid_2
4 txtName_4 ddlType_4 chkValid_4
5 txtName_5 ddlType_5 chkValid_5

现在我想在 for 循环中迭代这些控件(1 到“最后一行 ID”)以生成连接字符串。 如何在循环中跳过这些已删除的ID?

最佳答案

通过应用这篇 SO 帖子中的概念 How to check if element exists in the visible DOM ,可以在循环中按如下方式检查跳过编号的控件ID

var i;
for (i = 1; i <= lastRowID; i++;)
{
var element = document.getElementById('txtName'+i);
if (typeof(element) != 'undefined' && element != null)
{
// control with this ID exists.
} else {
// control with this ID does NOT exist, SKIP it.
}
}

关于javascript - ID 中具有跳过序列的动态控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33171473/

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