2016-06-07 79 views
1

所以我在左边有一个大按钮,右边有一个表格。当您单击该按钮时,最多可创建5个附加表单。它还更新了ID和风味档案#文本。使用删除按钮单击克隆div - 但是,如何隐藏初始克隆上的删除按钮?

它仍然有点bug,并寻找一些帮助整理出来,因为即时通讯不是最好的。

问题1:如果您创建5个额外的克隆,然后删除它们。当你再次创建它们时,它将它们标记为#7#8#9 - 由于只允许有6种形式。我需要这个号码只显示1-6,而不是上面或下面。我也想为id做同样的事情。

问题2:另一个问题我是我想删除“删除链接”风味曲线#1(第一种形式)。因为如果所有表格都被删除了,那么就没有什么可以克隆的了。

感谢您的帮助!

JS FIDDLE

var cloneIndex = 1; 
var clones_limit = 5; 
var cloned_nbr = $(".clonedInput").length-1; //Exclude Default (first) div 

function clone() 
{ 
    if(cloned_nbr<clones_limit) 
    { 
    cloneIndex++; 
    cloned_nbr++; 

    var new_clone = $(".clonedInput").first().clone(); 

    new_clone.attr("id", "clonedInput" + cloneIndex); 
    new_clone.find(".label-nbr").text(cloneIndex); 
    new_clone.find(".category").attr("id","category"+cloneIndex); 
    new_clone.show(".remove").attr("id","remove"+cloneIndex); 
    new_clone.on('click', 'button.clone', clone); 
    new_clone.on('click', 'button.remove', remove); 

    $("#formy").append(new_clone); 
    } 
} 
function remove(){ 
    if(cloneIndex>1){ 
    $(this).parents(".clonedInput").remove(); 
    cloned_nbr--; 
    } 
} 
$(".clone").on("click", clone); 

$(".remove").on("click", remove); 

回答

1

我改变了一下。它应该使用适当的索引并删除按钮!

function getFreeIds() { 
    var used = $('.clonedInput').find('.label-nbr').map(function(i, v) { 
      return parseInt(v.innerText) 
     }).get(); 
    return allowed.filter(function (i) {return used.indexOf(i) === -1}); 
} 

它做你所需要的。

http://jsfiddle.net/tfFLt/1921/

+0

删除2项不工作http://jsfiddle.net/tfFLt/ 1919/ – SamGhatak

+0

@SamGhatak错误的链接队友。 – wot

+1

哈哈没有问题...你编辑它4-5次我猜!感到困惑,欢呼声 – SamGhatak

1

添加,检查可拆卸div的现在的数目的函数。如果其超过1,显示删除按钮,否则不执行:

function handleRemoveButton(){ 
    var numItems = $('.clonedInput').length; 
    if(numItems<=1){ 
     $(".remove").hide(); 
    } 
    else{ 
     $(".remove").show(); 
    } 
} 

三而竭称之为:在最后的clone(){}remove()一次在$(document).ready();一次。

+0

不知道如何添加到我的预先存在的代码是诚实的。只是试图没有运气。 – Patrick

+0

你可以用你的改变重新发布我的JS小提琴吗? :) – Patrick

+0

好吧,让我把工作小提琴 – SamGhatak

1

写一个重新排列功能更新内容,并从当你克隆一个项目或删除它

function rearrange(){ 
    var count = 1; 
    var totalCount = $(".clonedInput").length; 
    $(".clonedInput").each(function(){ 
     $(this).attr("id", "clonedInput"+count).find(".label-nbr").text(count).end(). 
     find(".category").attr("id","category"+count).end().find(".remove").toggle(totalCount!=1).attr("id","remove"+count).on("click", remove); 
     count++; 
    }); 
} 

叫它检查的jsfiddle在:http://jsfiddle.net/tfFLt/1922/

+0

欢迎!它对你的帮助很大。 –