【问题标题】:Cloning divs on click with a remove button - But how do i hide the remove button on the initial clone?使用删除按钮单击克隆 div - 但是如何在初始克隆上隐藏删除按钮?
【发布时间】:2016-10-07 02:52:03
【问题描述】:

所以我在左边有一个大按钮,在右边有一个表单。当您单击该按钮时,它会创建多达 5 个附加表单。它还会更新 id 和风味配置文件 # 文本。

它仍然有一点问题,正在寻求一些帮助来解决它,因为我不是最好的 JS。

问题 1: 如果您创建了 5 个额外的克隆,然后将其删除。当您再次创建它们时,它会将它们标记为 #7 #8 #9 - 因为只允许使用 6 个表单。我需要这个数字来只显示 1-6 而不能高于或低于。 我也想为 id 做同样的事情。

问题 2: 我遇到的另一个问题是我想从 Flavor Profile #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);

【问题讨论】:

    标签: javascript jquery html clone


    【解决方案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/

    【讨论】:

    • 哈哈没问题...我猜你编辑了 4-5 次!!很困惑,干杯
    • 看起来更好,如果在remove的末尾加上renumbering功能会更好吗? 1,2,3,4 on remove 1 and add 现在给出 2,3,4,1....如果能把它排序就好了...不过是很好的解决方案 (y)
    • 我们不能为他做所有事情哈哈,他还需要学习:)
    • 你为我做的一切我都很好! :D 如果你愿意,我会把它变成另一个问题。 xD
    【解决方案2】:

    添加一个函数来检查存在的可移动divs 的数量。如果大于 1,则显示删除按钮,否则不显示:

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

    并调用它三次:一次在$(document).ready(); 上,一次在clone(){}remove() 的最后一次。

    【讨论】:

    • 老实说,不确定如何添加到我现有的代码中。只是没有运气。
    • 你能重新发布我的 JS 摆弄你的更改吗? :)
    • 谢谢!! :)(有没有办法让我在第一个按钮上隐藏实际按钮?)
    【解决方案3】:

    编写一个重新排列函数来更新内容并在克隆或删除项目时调用它

    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/

    【讨论】:

    • 欢迎您!对你有帮助真是太好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 2016-04-09
    • 2017-04-28
    • 2012-12-02
    • 2021-08-02
    • 2020-07-27
    相关资源
    最近更新 更多