【问题标题】:How to change the attributes of page elements when cloning in jquery?在jquery中克隆时如何更改页面元素的属性?
【发布时间】:2014-09-02 20:07:38
【问题描述】:

例子:

http://jsbin.com/naqaboga/1/edit?html,js,output

克隆“hidden”属性时需要改变name = "" and value = "" for input, select, and textarea on value的i。谢谢。

(点击)name="data[group][text][hidden]" to name="data[group][text][0]"

(点击)name="data[group][text][hidden]" to name="data[group][text][1]"

【问题讨论】:

  • 将克隆元素存储在某个 var 中并更改属性,发布您尝试过的代码以提供更好的帮助。

标签: jquery input clone attr


【解决方案1】:

您需要将元素的这些属性设置为:

$(document).ready(function () {
    $('#group').on('click', '.add_subgroup', function () {
        var i = $('div.subgroup').length;
        var where = $('#group');
        var subgroup = $('#subgroup_hidden').clone();

        var j = i + 1;
        subgroup.attr({
            'id': 'subgroup_' + j,
            'class': 'subgroup'
        }).appendTo(where);

        subgroup.find('input, select, textarea').each(function(){
            $(this).attr({
                name: '',
                value: ''
            })
            if($(this).is('textarea'))
                $(this).text('');
        })
        i++;
    });

    $('#group').on('click', '.remove_subgroup', function () {
        $(this).closest('.subgroup').remove();
    });
});

working fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多