我正在尝试通过创建唯一 ID 将行克隆到内容中,但我遇到了一些问题。
HTML:
<div class="content">
<div id="email-container" class="email-container">
<input type="text" value="" placeholder="Email">
<input type="text" value="" placeholder="Email">
<input type="text" value="" placeholder="Email">
<span class="plus-button">+</span>
</div>
</div>
JavaScript:
$('.plus-button').click(function(){
$('.email-container').clone().insertAfter(".email-container").end();
$('.email-container').find('.plus-button').text('-').not(':last-child');
});
请在下面找到我的 fiddle :
http://jsfiddle.net/meetravi/GCRVS/3/
我正在尝试克隆 #email-container
并将其添加到 .content
中,并清除文本框值,还需要更改 .plus-button
值为“-”(最后一行除外)。
最佳答案
试试这个: 而且您不需要 id #email-container。
$(document).on('click', '.plus-button', function(){
var content = $(this).text();
if(content == '+'){
$(this).parent().clone(true, true).insertAfter($(this).parent());
$('.email-container').each(function(){
if(!$(this).is(':last-child')){
$(this).find('.plus-button').html('-');
}
});
}
else{
$(this).parent().remove();
}
});
编辑:我进行了重大编辑,插入了 if/else 来解决删除问题...
EDIT2:必须说 insertAfter($(this).parent()
关于javascript - jQuery 克隆问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12037077/