gpt4 book ai didi

jquery - 克隆形式和增量

转载 作者:行者123 更新时间:2023-12-01 01:55:48 25 4
gpt4 key购买 nike

我有一个下面的表单,我想克隆它,增加 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>

最佳答案

请参阅fiddle .

将类(class)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
});

关于jquery - 克隆形式和增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4400592/

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