简体   繁体   English

JavaScript 阵列在拼接时未正确移除

[英]JavaScript Array not remove properly in splice

I am doing a list of input it's working but when I delete the item and submit again it does changes index and add the new items in the wrong position.我正在做一个输入列表,但当我删除该项目并再次提交时,它会更改索引并将新项目添加到错误的 position 中。 Here is a live example https://codesandbox.io/s/flamboyant-curie-bb6yn?file=/src/index.js .这是一个实时示例https://codesandbox.io/s/flamboyant-curie-bb6yn?file=/src/index.js Let me know if you need any changes and also if I remove images code and test plain text form it still showing same error.让我知道您是否需要任何更改,以及如果我删除图像代码并测试纯文本表单,它仍然显示相同的错误。

 const Data = [ "https://romeojeremiah.github.io/Filter-Project/img/sweets-1.jpeg", "https://romeojeremiah.github.io/Filter-Project/img/sweets-2.jpeg", "https://romeojeremiah.github.io/Filter-Project/img/sweets-3.jpeg", "https://romeojeremiah.github.io/Filter-Project/img/doughnut-1.jpeg", "https://romeojeremiah.github.io/Filter-Project/img/doughnut-2.jpeg", "https://romeojeremiah.github.io/Filter-Project/img/doughnut-3.jpeg", ]; const submitBtn = document.querySelector(".submitBtn"); let showLists = document.querySelector(".showLists"); let arry = []; let count = 0; submitBtn.addEventListener("click", () => { const name = document.querySelector(".name").value; const course = document.querySelector(".course").value; const author = document.querySelector(".author").value; count++; if (count === 6) { count = 0; } let x = ` <div class="col-sm-4 mb-3"> <img src=${Data[count]} class="img-fluid" /> <h4>Name: ${name} </h4> <h4>Course: ${course}</h4> <h4>Author: ${author}</h4> <button type="submit" class="btn btn-primary deleteBtn"> Delete </button> </div> `; arry.push(x); showLists.innerHTML = arry.join(" "); deleteBtn(); }); const deleteBtn = () => { const deleteBtn = document.querySelectorAll(".deleteBtn"); deleteBtn.forEach((item, index) => { item.addEventListener("click", (e) => { arry.splice(index, 1); e.target.parentElement.remove(); }); }); };
 <div class="row no-gutters"> <div class="container"> <div class="col-sm-12 mt-3"> <input type="text" id="myInput" class="form-control name" placeholder="Name" title="Type in a name" /> <input type="text" id="myInput" class="form-control my-3 course" placeholder="Course" title="Type in a name" /> <input type="text" id="myInput" class="form-control author" placeholder="Author" title="Type in a name" /> <div class="my-3"> <button class="btn btn-secondary submitBtn">SUBMIT</button> </div> <div class="showLists row"></div> </div> </div>

I removed arry variable because I don't see point of keeping this variable.我删除了arry变量,因为我没有看到保留这个变量的意义。 With innerHtml you redefine whole content of container .showLists , so you must apply listeners again to all buttons.使用innerHtml您重新定义了容器.showLists的全部内容,因此您必须再次将侦听器应用于所有按钮。 This is not optimal solution.这不是最佳解决方案。 You should consider replacing it for example appendChild .您应该考虑替换它,例如appendChild

const Data = [
  "https://romeojeremiah.github.io/Filter-Project/img/sweets-1.jpeg",
  "https://romeojeremiah.github.io/Filter-Project/img/sweets-2.jpeg",
  "https://romeojeremiah.github.io/Filter-Project/img/sweets-3.jpeg",
  "https://romeojeremiah.github.io/Filter-Project/img/doughnut-1.jpeg",
  "https://romeojeremiah.github.io/Filter-Project/img/doughnut-2.jpeg",
  "https://romeojeremiah.github.io/Filter-Project/img/doughnut-3.jpeg"
];

const submitBtn = document.querySelector(".submitBtn");
let showLists = document.querySelector(".showLists");
let count = 0;

submitBtn.addEventListener("click", () => {
  const name = document.querySelector(".name").value;
  const course = document.querySelector(".course").value;
  const author = document.querySelector(".author").value;
  count++;
  if (count === 6) {
    count = 0;
  }

  let x = `
  <div class="col-sm-4 mb-3">
        <img
          src=${Data[count]}
          class="img-fluid"
        />

          <h4>Name : ${name} </h4>
          <h4>Course : ${course}</h4>
          <h4>Author : ${author}</h4>


        <button type="submit" class="btn btn-primary deleteBtn">
          Delete
        </button>
      </div>

  `;

  showLists.innerHTML += x;
  deleteBtn();
});

let deleteBtn = () => {
  let deleteBtn = document.querySelectorAll(".deleteBtn");
  console.log(deleteBtn);
  console.log(deleteBtn.length-1);
  deleteBtn.forEach((item) => {
    item.addEventListener("click", (e) => {
      e.target.parentElement.remove();
    });
  });
};

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

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