简体   繁体   English

在按钮上更新数组单击Javascript

[英]Updating an array on button click Javascript

This question may be a bit long winded but bear with me. 这个问题可能有点啰嗦,但请耐心等待。

I am trying to update and array every time a user hits the save button. 每次用户点击保存按钮时,我都会尝试更新和排列数组。

When they click save an image of a canvas on the page is created. 单击“保存”时,将创建页面上的画布图像。

These DataURI values are kept in an array. 这些DataURI值保存在一个数组中。

Once the value is saved a thumbnail of sorts is created and added at the bottom of the screen. 保存该值后,将在屏幕底部创建并添加各种缩略图。

Clicking the X icon on those images calls a function to remove the correct image from the array. 单击这些图像上的X图标会调用一个函数来从阵列中删除正确的图像。

The images should then be redrawn with the update array values, thus removing it from the screen. 然后应使用更新数组值重新绘制图像,从而将其从屏幕中删除。

I have included images to try and demonstrate: 我已经包含图像来尝试和演示:

Image #1 (when save is clicked and image added below): 图像#1(单击保存并在下面添加图像时):

http://postimg.org/image/cybazwydf/ http://postimg.org/image/cybazwydf/

Image #2 (after closing the on screen images, adding a new image adds the deleted ones again along with the new one): 图像#2(在关闭屏幕图像后,添加新图像会再次添加已删除的图像以及新图像):

http://postimg.org/image/gi5pcornl/ http://postimg.org/image/gi5pcornl/

That is the issue, that it re-adds the deleted values. 这是问题,它重新添加已删除的值。

I will post the code for it below: 我将在下面发布它的代码:

function getDataUrl () {
var a = document.getElementById("theCanvas");
var context = a.getContext("2d");
var dataURL = a.toDataURL();
save(dataURL);
}


var theImages = new Array();




//Add dataURL to array:
function save(URL) {
    theImages.push(URL);
    var x = JSON.stringify(theImages);
    localStorage.setItem('images', x);


drawImages(x);
}

function drawImages(array){

    var array = localStorage.getItem("images");
    array = JSON.parse(array);

    //If an image is saved, display the saveArea div:
    if (array.length > 0){
        document.getElementById("saveArea").style.visibility="visible";
    }

    //Clear the elements that might already be in the div so they don't appear twice:
    var theDiv = document.getElementById("saveArea");
    while (theDiv.firstChild) {
    theDiv.removeChild(theDiv.firstChild);
    }


    for (var x=0; x < array.length; x++){ 
                    //Create image for each value in array:



                    var divimg = document.createElement("div");

                    divimg.style.marginRight="10px";
                    //divimg.style.border = "1px dotted red";
                    divimg.className = "saveContainer";
                    divimg.style.width = 300+"px";
                    divimg.style.padding = 5+"px";
                    divimg.style.marginRight="10px";
                    divimg.style.height = 150+"px";
                    divimg.style.display="inline-block";    
                    divimg.style.marginRight="35px";


                    document.getElementById("saveArea").appendChild(divimg);

                    var img = document.createElement("img");
                    img.src = array[x];
                    img.width = 300;
                    img.height = 150;
                    img.setAttribute("id", "theImageId");
                    img.style.marginRight="10px";
                    img.className = "saveImg";


                    //Add each image to the containing div:
                    divimg.appendChild(img);




                    //Create close button: 
                    var close = document.createElement("img");
                    close.src="close.png";
                    close.width = 50;
                    close.height = 50;
                    close.border = 0;
                    close.style.position="relative";
                    close.style.bottom=115+"px";
                    close.style.right=40+"px";
                    close.className="closeButton";
                    //close.style.cssFloat="right";
                    //close.style.right= 0+"px";


                    var link = document.createElement("a");
                    link.href = "#";

                    link.appendChild(close);

                    link.nameIndex = x;

                    //WHEN THE USER CLICKS THE CLOSE ICON:
                    link.onclick = (function (x) {
                    var imageNum = this.nameIndex;
                    alert("You clicked to close image "+(imageNum+1));
                        //Remove the image:
                        array.splice(x,1);

                        alert("The length of this array is: "+array.length);
                        //Update localStorage:
                        localStorage.removeItem('images');
                        array = JSON.stringify(array);

                        localStorage.setItem('images', array);
                        drawImages(array);
                    } );




                    //Add the close button the the containing div:
                    divimg.appendChild(link);
                    //divimg.appendChild(close);

    } //End Loop

} //End drawImages();

I've been trying to solve this for hours but no luck.. 我一直试图解决这个问题几个小时但没有运气..

After removing the image from the array you are not storing it anywhere so the splice result is lost and the array remains the same 从阵列中删除图像后,您不会将其存储在任何位置,因此拼接结果将丢失并且阵列保持不变

    array.splice(x,1);

needs to be 需要是

    array =  array.splice(x,1);

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

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