简体   繁体   English

Javascript - 从动态创建的数组中删除特定元素

[英]Javascript - remove specific element from dynamically created array

I have a page where users can create tags (much like here in stackoverflow), which are then sent(POST) to the back end to be stored in a database. 我有一个页面,用户可以在其中创建标签(就像在stackoverflow中一样),然后将其发送(POST)到后端以存储在数据库中。 The user can make tags but also remove them before finally hitting Submit. 用户可以制作标签,但也可以在最终点击提交之前将其删除。

In the DOM the tags are generated along with an 'x' button. 在DOM中,标签与“x”按钮一起生成。 The 'x' button removes the element from the DOM, but the trouble comes when removing from the array. 'x'按钮从DOM中删除元素,但从数组中删除时会出现问题。 The closest I could get to a solution was this question , however I couldn't get it to quite work for me. 我能找到最接近解决方案的是这个问题 ,但是我无法让它为我工作。

Here's the codepen 这是codepen

Here's the javascript (i'm using JQuery) 这是javascript(我正在使用JQuery)

window.tag_array = [];

$( "#addtag" ).click(function() {

var tag = $("#input-tag").val();

//if tag is empty
if(!$('#input-tag').val()) {

    alert("can't be empty");

    } else {
        //put tag.val into an array         
        tag_array.push(tag);

        //add to DOM
        $( "#tagsbox" )
        .append( "<div class='displaytag'><i>"+tag+"</i><input type='hidden' class='tag' value="+tag+"><button onClick='return false;' class='removetag'>x</button></div>" );

        //reset value in text area to null
        $("#input-tag").val("");

        //remove tag onclick
        $('.removetag').click(function() {
            $(this).parent().remove(); //remove tag from DOM

            //splice from array
            tag_array.splice( this, 1 ); //<--HERE IS PROBLEM (i think)

        });


    } //end else

    alert(tag_array); //check array
});

The end result is the splice takes out too many array items. 最终结果是拼接取出了太多的数组项。

I have also tried 我也试过了

tag_array.splice(tag_array.indexOf(tag),1);

to a similar result. 得到类似的结果。

Please help! 请帮忙! Thanks in advance 提前致谢

You should probably use something like .indexOf() to get an index of the element and then splice an array: 你应该使用类似.indexOf()东西来获取元素的索引,然后拼接一个数组:

tag_array.splice(tag_array.indexOf(elm),1);

Working demo 工作演示

The splice part is OK. splice部分没问题。 The problem is that you're adding a click callback to .removetag too many times. 问题是你要多次向.removetag添加点击回调。

Everytime you append a new element, you are adding another click event to every .removetag item that is already on the page. 每次附加新元素时,都会向页面上已有的每个.removetag项添加另一个click事件。

$('.removetag').click(function()

This way, whenever you click on one element, all the others were assign to fire the click callback too. 这样,每当您单击一个元素时,所有其他元素都被分配以触发单击回调。

Solution

Instead, when creating the tag, set the click event only to the last added .removetag element: 相反,在创建标记时,仅将click事件设置为最后添加的.removetag元素:

$('.removetag').last().click(function()

Updated CODEPEN 更新了CODEPEN

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

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