克隆形式和增量

发布于 2024-10-07 00:55:47 字数 2074 浏览 0 评论 0原文

我有一个下面的表单,我想克隆它,增加 id(att1) 及其输入、文本区域等,然后附加到与会者 div 中。非常感谢任何帮助。一直在伤透我的头......

我所拥有的只是......

$(function () {
        var index = 1;


    $(".add").click(function () {
            index++;

        $("#att1").clone(true).removeAttr("id").attr("id", "att" + index).appendTo("#attendees");
            alert(index);
    });
});

Html:

    <div id="attendees">
            <div id="att1">
                    <fieldset>
                        <legend><span class="legend">Attendee 1 Booking Details</span></legend>
                        <p name="test">
                        <label for="A_Title_1">Title: <span class="req">*</span></label>
                        <input name="A_Title_1" id="A_Title_1" value="" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Forename_1">Forename: <span class="req">*</span></label>
                        <input name="A_Forename_1" id="A_Forename_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Surname_1">Surname: <span class="req">*</span></label>
                        <input name="A_Surname_1" id="A_Surname_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Info_1">Additional Info: </label>
                        <textarea name="A_Info_1" id="A_Info_1" cols="20" rows="10"></textarea>
                        <span class="info">Please include any infomation relating to dietary/ access/special requirements you might have.</span>
                        </p>
                    </fieldset>
                    </div>
<a href="#" class="add">Add more</a>

    </div>

I have a form below that I want to clone, increment the id(att1) and it's inputs, textareas etc and append to the attendees div. Any help much appreciated. Been wrecking my head...

All I have is...

$(function () {
        var index = 1;


    $(".add").click(function () {
            index++;

        $("#att1").clone(true).removeAttr("id").attr("id", "att" + index).appendTo("#attendees");
            alert(index);
    });
});

Html:

    <div id="attendees">
            <div id="att1">
                    <fieldset>
                        <legend><span class="legend">Attendee 1 Booking Details</span></legend>
                        <p name="test">
                        <label for="A_Title_1">Title: <span class="req">*</span></label>
                        <input name="A_Title_1" id="A_Title_1" value="" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Forename_1">Forename: <span class="req">*</span></label>
                        <input name="A_Forename_1" id="A_Forename_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Surname_1">Surname: <span class="req">*</span></label>
                        <input name="A_Surname_1" id="A_Surname_1" type="text" class="f_input" />
                        </p>

                        <p>
                        <label for="A_Info_1">Additional Info: </label>
                        <textarea name="A_Info_1" id="A_Info_1" cols="20" rows="10"></textarea>
                        <span class="info">Please include any infomation relating to dietary/ access/special requirements you might have.</span>
                        </p>
                    </fieldset>
                    </div>
<a href="#" class="add">Add more</a>

    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱人如己 2024-10-14 00:55:47

请参阅小提琴

将类 attendee 添加到 div id="att1"

 <div id="att1" class="attendee">

和 JS:

$(function(){
    var template = $('#attendees .attendee:first').clone(),
        attendeesCount = 1;

    var addAttendee = function(){
        attendeesCount++;
        var attendee = template.clone().find(':input').each(function(){
            var newId = this.id.substring(0, this.id.length-1) + attendeesCount;
            $(this).prev().attr('for', newId); // update label for (assume prev sib is label)
            this.name = this.id = newId; // update id and name (assume the same)
        }).end() // back to .attendee
        .attr('id', 'att' + attendeesCount) // update attendee id
        .prependTo('#attendees'); // add to container
    };

    $('.add').click(addAttendee); // attach event
});

See the fiddle.

Add a class attendee to the div id="att1"

 <div id="att1" class="attendee">

And the JS:

$(function(){
    var template = $('#attendees .attendee:first').clone(),
        attendeesCount = 1;

    var addAttendee = function(){
        attendeesCount++;
        var attendee = template.clone().find(':input').each(function(){
            var newId = this.id.substring(0, this.id.length-1) + attendeesCount;
            $(this).prev().attr('for', newId); // update label for (assume prev sib is label)
            this.name = this.id = newId; // update id and name (assume the same)
        }).end() // back to .attendee
        .attr('id', 'att' + attendeesCount) // update attendee id
        .prependTo('#attendees'); // add to container
    };

    $('.add').click(addAttendee); // attach event
});
诠释孤独 2024-10-14 00:55:47

试试这个

    empty_html = $('#att1').html();

    $('.add').click(function() {

        last_id = $('#attendees div:last').attr('id').replace('attr','');

        next_id = last_id++;

        new_div = $('<div id="' + next_id + '">' + empty_html +'<div>');

        $('#attendees').append(new_div);

    });

需要对您的 HTML 进行一些调整,我会将添加链接移出与会者 div。

Try this

    empty_html = $('#att1').html();

    $('.add').click(function() {

        last_id = $('#attendees div:last').attr('id').replace('attr','');

        next_id = last_id++;

        new_div = $('<div id="' + next_id + '">' + empty_html +'<div>');

        $('#attendees').append(new_div);

    });

Requires a few tweaks to your HTML, I would move the add link out of the attendees div.

爱要勇敢去追 2024-10-14 00:55:47

这个例子可能对你有帮助。 示例

$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $(document).on('click','#addScnt', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });

    $(document).on('click','#remScnt', function() { 
            if( i > 2 ) {
                    $(this).parents('p').remove();
                    i--;
            }
            return false;
    });

});

This example might help you. example

$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $(document).on('click','#addScnt', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });

    $(document).on('click','#remScnt', function() { 
            if( i > 2 ) {
                    $(this).parents('p').remove();
                    i--;
            }
            return false;
    });

});
关于从前 2024-10-14 00:55:47

我知道这是旧线程..我只是想分享增加 id 的简单方法。我在所有克隆元素

更新中使用相同的类:

这种更优雅的方式:

var clone_counter = 1; // global variable
$('.clone').click(function(){
   $('.clonethis').clone();
   console.log(clone_counter);
   clone_counter++;
});

i know it's old thread.. i just wanna share simply way to increment id. i'm using same class in all cloned element

UPDATE :

this more elegance way :

var clone_counter = 1; // global variable
$('.clone').click(function(){
   $('.clonethis').clone();
   console.log(clone_counter);
   clone_counter++;
});
沧桑㈠ 2024-10-14 00:55:47

我在其他地方发布了相同的答案,但在这里我认为它也适用于这个问题。

我创建了一个“原始”元素并将其隐藏在页面上。我将“---”附加到需要在原始元素的任何子元素中递增的任何属性的值。

每当用户单击按钮来创建原始版本的克隆时,我都会创建一个克隆,然后将“---”全局替换为该克隆的 HTML 中的当前索引。像这样的事情:

var $clone = $original.clone();
var cloneHTML = $clone.html().replace(/---/g, incrementedFieldNum);
$clone.html(cloneHTML);

// Insert and show the clone after the original
$original.after($clone);

我使用数据属性来存储incrementedFieldNum的更新值(上面的代码中未显示)。

希望这有帮助。它的代码少了很多,我认为这是一个更通用的解决方案。我有很多嵌套元素需要具有递增的 ID 和名称,因此像 @Fierceblood 中的解决方案这样的解决方案是不切实际的。

I posted this same answer elsewhere, but here I think it applies to this question too.

I created an 'original' element and hid it on the page. I appended "---" to the value of any attribute that needed to be incremented within any child elements of the original.

Whenever the user clicked a button to create a clone of the original, I created a clone, then globally replaced "---" with the current index within that clone's HTML. Something like this:

var $clone = $original.clone();
var cloneHTML = $clone.html().replace(/---/g, incrementedFieldNum);
$clone.html(cloneHTML);

// Insert and show the clone after the original
$original.after($clone);

I used a data attribute to store updated values of incrementedFieldNum (not shown in the code above).

Hopefully that helps. It's a lot less code and it's a more general solution I think. I had a lot of nested elements that needed to have incremented IDs and names, so a solution like the one from @Fierceblood was impractical.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文