简体   繁体   English

Javascript添加/删除唯一克隆的div

[英]Javascript Add/Remove unique cloned div

I have a dropdown that I want to be cloned and have a unique id. 我有一个要克隆的下拉列表,并且具有唯一的ID。 I managed to do it and it works on my website. 我设法做到了,它可以在我的网站上运行。

I am trying to make a "x" button to removed added clones and I can't get it to work. 我正在尝试使用“ x”按钮来删除添加的克隆,但无法正常工作。

The javascript: JavaScript:

var counter = 1;
function addInput(divName, template){
    if (counter == 5)  {
        document.getElementById("add_more_text").remove();
    } else {
        var newdiv = document.createElement('div');
        newdiv.innerHTML = document.getElementById(divName).innerHTML;
        document.getElementById(template).appendChild(newdiv);
        counter++;
    }

    var selectElements = document.querySelectorAll('select');
    for (var i = 0; i < selectElements.length; i++){
        selectElements[i].id = 'id-' + i;
        selectElements[i].name = 'category' + i;
    }
}
function removeInput(divName, template){
    document.getElementById(template).removeChild(divName);
    counter--;
}

The html: 的HTML:

<div id="template">
    <select name="category0"><option>hi</option></select> 
    <a href="javascript:void(0)" onClick="removeInput('template', 'add_more');">x</a>                                       
</div>
<div id="add_more"></div>
<a href="javascript:void(0)" onClick="addInput('template', 'add_more');" id="add_more_text">+ Add more</a>

DEMO DEMO

Any help is much appreciated! 任何帮助深表感谢!

Simpler to modify remove function as follows: 修改删除功能更简单,如下所示:

function removeInput(obj) {
    if (obj.parentNode.className == 'added') {
        obj.parentNode.parentNode.removeChild(obj.parentNode);
        counter--;
    }
}

And have a link in template like this: 并在模板中有这样的链接:

<a href="javascript:void(0)" onClick="removeInput(this);">x</a> 

Class added is to distinguish new clones that can be removed: added类是为了区分可以删除的新克隆:

newdiv.className = 'added';

Demo: http://jsfiddle.net/rjXXa/2/ 演示: http//jsfiddle.net/rjXXa/2/

in your onClick property 在您的onClick属性中

<a href="javascript:void(0)" onClick="removeInput('template', 'add_more');">x</a>

you are passing template and add_more 您正在传递templateadd_more

And in the handler function 并在处理函数中

function removeInput(divName, template){

the parameters are in a different order, so divName will contain 'template' and template will contain 'add_more' . 参数的顺序不同,因此divName将包含'template'template将包含'add_more' Even if you fix this, 即使您解决了这个问题,

document.getElementById(template).removeChild(divName); // will throw error

because the div#add_more is not a child of div#template . 因为div#add_more不是div#template

For fixing this, you need to pass a reference to the clicked element, like the following 为了解决这个问题,您需要传递对clicked元素的引用,如下所示

<a href="javascript:void(0)" onClick="removeInput(this);">x</a> 

and in your function 而在你的职能

function removeInput(anchor){ 
 var clone = anchor.parentNode; // div containing the anchor
 if(clone.id!='template'){ // make sure we're not removing the original template 
  clone.parentNode.removeChild(clone);
  counter--;
 }
}

as in this Fiddle 就像这个小提琴

Update 更新

It's better to remove the add more option from display using css and make it visible later than removing/appending it in DOM, as follows 最好使用CSS从显示中删除add more选项,并使其在以后可见,而不是在DOM中删除/附加它,如下所示

change the following in addInput() function addInput()函数中更改以下内容

if (counter > 4)  {
    document.getElementById("add_more_text").style.display='none';
}

and in removeInput() function add 并在removeInput()函数中添加

 if (counter < 5)  {
    document.getElementById("add_more_text").style.display='block';
 }

as in this Fiddle 就像这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM