0

在工作中,我有一些带有普通 html 元素和经典 asp 动态复选框的 html 表单(经典的 asp 背面)。

我们想要对此进行报告并创建数据透视表,因此我需要按照它们在表单上出现的顺序提供 html 对象(静态和动态)的所有名称或 ID(名称可能更好)的列表。有些是隐藏的,直到用户与依赖对象交互(我们也想要那些)。

我发现了多篇关于让所有人参加课程的帖子,但可以找到这个确切的答案,因为顺序非常重要!

jQuery 已经在页面上使用,因此可以很好地使用。

提前致谢!

4

1 回答 1

1

使用任一选项在 内创建元素 ID/名称列表#myForm

var orderedArrayId = [];
$("#myForm *[id]").each(function(){
    orderedArrayId.push(this.id);
})

var orderedArrayName = [];
$("#myForm *[name]").each(function(){
    orderedArrayName.push(this.name);
})

如果您想获取 DOM 元素的列表.push(this.id),您也可以使用,而不是 using 。.push(this)

运行此代码后,您可以遍历所有元素:

$.each(orderedArrayId, function(index, element){
    //element holds the ID of the element
    // (or a reference to the DOM element, if you've used `.push(this)` instead
    //      of `.push(this.id)`)
    //Do something
});
于 2011-10-04T21:16:30.633 回答