0

我使用下面的 jquery 来更改 jquery 迭代的所有名称属性值。我尝试将 id 值分配给 div 内所有控件的 name 属性,并且 div id 是Register。这是我的代码。

 $(document).ready(function () {
            $.each($('#Register').children(), function () {
                alert("pp");
                $(this).attr("name", $(this).attr("id"));
            });
        });

当我运行我的页面时,上面的脚本假设运行但它没有运行。所以请指导我在哪里犯了错误。谢谢

更新

最后这行得通

    $(document).ready(function () {
        $('#Register').find('*').each(function () {
            $(this).attr("name", $(this).attr("id"));
        });
    });
4

2 回答 2

1

试试这个.attr( attributeName, function(index, attr) ): -

$('#Register').children().attr("name", function (index, oldName) {
    return this.id || '';
});

小提琴演示

于 2013-10-07T12:46:10.587 回答
1

.children()只会给你直接的孩子(不是所有的后代)。

您应该使用.find('*')所有降序节点。

注意:您的代码将为name基节点的每个后代添加一个属性(包括divspan...)。如果你只想更新input节点,你应该添加一个足够的过滤器.find()

于 2013-10-07T12:58:59.840 回答