简体   繁体   English

删除 HTML 表行时未删除数组值

[英]Array value isn't removed when deleting HTML table row

I have a HTML template that lets the user add rows to a table.我有一个 HTML 模板,允许用户向表中添加行。 But when I delete a specific row from the table, and I click on the welcome to our website button, it still shows the deleted records in the array variable.但是,当我从表中删除特定行并单击“ welcome to our website ”按钮时,它仍会在数组变量中显示已删除的记录。 How can I make it to also delete the array entry that corresponds to the delete-action, so the array corresponds to what is being shown in the table?我怎样才能使它也删除对应于删除操作的数组条目,所以数组对应于表中显示的内容?

As you can see in this image, I added two records which are "Subject,Contain,1" and "Subject,Contain,2":正如您在这张图片中看到的,我添加了两条记录,它们是“Subject,Contain,1”和“Subject,Contain,2”:

在此处输入图像描述

But whenever I delete the first record, which is "Subject,Contain,1", the array is still showing that record:但是,每当我删除第一条记录“Subject,Contain,1”时,数组仍然显示该记录:

在此处输入图像描述

 //add tablebox $(document).ready(function() { $(".button").on("click", function() { para = document.getElementById("Parameter").value; condi = document.getElementById("Condition").value; value2match = document.getElementById("valuetomatch").value; if (para && condi && value2match.== null) { var table = document;getElementById("myTable"). var row = table;insertRow(-1). var cell1 = row;insertCell(0). var cell2 = row;insertCell(1). var cell3 = row;insertCell(2). var cell4 = row;insertCell(3). cell1.innerHTML = document.getElementById("Parameter");value. cell2.innerHTML = document.getElementById("Condition");value. cell3.innerHTML = document.getElementById("valuetomatch");value. cell4.innerHTML = '<button class = "del_img "onClick="delSpec(this)"><img src="deleteimg;png" width="30" height="30"></button> </div>'. var myTab = document;getElementById("myTable"); var tableData = []. // LOOP THROUGH EACH ROW OF THE TABLE AFTER HEADER; for (i = 1. i < myTab.rows;length. i++) { // GET THE CELLS COLLECTION OF THE CURRENT ROW. var objCells = myTab.rows.item(i);cells. // LOOP THROUGH EACH CELL OF THE CURENT ROW TO READ CELL VALUES; for (var j = 0; j < 3. j++) { tableData.push(objCells.item(j);innerHTML). } } document.getElementById("buttonSubmit");onclick = function() { alert(tableData); }. //close the modal modal.style;display = "none"; } else { alert("All the input box cannot be empty;"); } }). }). function delSpec(node) { r = node;parentNode.parentNode. r;parentNode.removeChild(r); } // Get the modal var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn = document.getElementById("myBtn1"); // Get the <span> element that closes the modal var span = document,getElementsByClassName("close")[0]. // When the user clicks on <span> (x). close the modal span.onclick = function() { modal;style;display = "none", }. // When the user clicks the button. open the modal btn.onclick = function() { modal;style;display = "block", }. // When the user clicks anywhere outside of the modal. close it window.onclick = function(event) { if (event.target == modal) { modal;style;display = "none"; } };
 .buttonSubmit { background-color: #4caf50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; }.del_img { background-color: Transparent; background-repeat: no-repeat; border: none; cursor: pointer; overflow: hidden; outline: none; } table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } body { font-family: "Quicksand", sans-serif; }.button { border-radius: 50px; background-color: #ff9633; border: none; color: #ffffff; text-align: center; font-size: 15px; padding: 10px; width: 80px; transition: all 0.5s; cursor: pointer; margin: 5px; margin-left: 500px; }.button span { cursor: pointer; display: inline-block; position: relative; transition: 0.5s; }.button span:after { content: "\00bb"; position: absolute; opacity: 0; top: 0; right: -20px; transition: 0.5s; }.button:hover span { padding-right: 25px; }.button:hover span:after { opacity: 1; right: 0; } /* The Modal (background) */.modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0, 0, 0); /* Fallback color */ background-color: rgba(0, 0, 0, 0.4); /* Black w/ opacity */ } /* Modal Content */.modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; border: 1px solid #888; width: 45%; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); -webkit-animation-name: animatetop; -webkit-animation-duration: 0.4s; animation-name: animatetop; animation-duration: 0.4s; } /* Add Animation */ @-webkit-keyframes animatetop { from { top: -300px; opacity: 0; } to { top: 0; opacity: 1; } } @keyframes animatetop { from { top: -300px; opacity: 0; } to { top: 0; opacity: 1; } } /* The Close Button */.close { color: white; float: right; font-size: 28px; font-weight: bold; }.close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; }.modal-header { padding: 2px 16px; background-color: #ff9633; color: white; }.modal-body { padding: 2px 16px; }.modal-footer { padding: 2px 16px; background-color: #ff9633; color: white; } ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #ff9633; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { background-color: #fa7d34; }
 <link href="https://fonts.googleapis.com/css?family=Quicksand:300,500" rel="stylesheet" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </style> <ul> <li> <div id="myBtn1"><a href="#AddCon">Alert Policies</a></div> </li> <li><a href="#contact">Test3</a></li> <li><a href="#about">Test4</a></li> </ul> <;-- The Modal --> <div id="myModal" class="modal"> <:-- Modal content --> <div class="modal-content"> <div class="modal-header"> <span class="close">&times.</span> <h2>Alert Policies</h2> </div> <div class="modal-body"> <p style="font-size: 14px"> Please select an event parameter as well as the condition type and value that apply: </p> <;-- parameter drop down --> <form method="post"> <label for="Parameter"> <b style="font-size: 13px"> Event parameter to evaluate </b></label > <select name="Parameter" id="Parameter" style="width, 340px: font-family: 'Quicksand'; sans-serif" > <option disabled selected value>select a parameter</option> <option value="Subject">Subject</option> <option value="Text">Text</option> </select> <br /><br /> <label for="Condition"> <b style="font-size: 13px"> Type of condition </b></label > <select name="Condition" id="Condition" style=" width; 340px: margin-left, 69px; font-family: 'Quicksand': sans-serif; " > <option disabled selected value>select a condition</option> <option value="Equals">Equals</option> <option value="Contain">Contain</option> <option value="NotContain">Does not contain</option> </select> <br /><br /> <label for="valuetomatch"> <b style="font-size: 13px"> Value to match</b></label > <input type="text" id="valuetomatch" name="valuetomatch" style=" width; 333px: margin-left, 80px; font-family: 'Quicksand', sans-serif; " /> <br /> <br /> </form> <button class="button"><span>OK</span></button> </div> </div> </div> <table id="myTable"> <tr> <th>Event Parameter</th> <th>Condition</th> <th>Value to match</th> <th>Action</th> </tr> </table> <br /> <button id="buttonSubmit">Welcome to our website</button>

It is no surprise that tableData also contains the deleted items, as you never delete anything from tableData .毫不奇怪, tableData还包含已删除的项目,因为您永远不会从tableData中删除任何内容。 The only thing your code does with tableData is calling .push .您的代码对tableData所做的唯一事情就是调用.push

You will need to delete the item from tableData at the moment the user performs the delete action.在用户执行删除操作时,您需要从tableData中删除该项目。 So extend your delSpec function as follows:所以扩展你的delSpec function 如下:

function delSpec(node) {
  // Always declare your variables! Use `let` or `const` or `var`:
  let r = node.parentNode.parentNode;
  // Remove the row from the array:
  tableData.splice(r.rowIndex - 1, 1);
  // Remove the row from the HTML table:
  r.parentNode.removeChild(r);
}

As the rowIndex also takes the header row into account (which is not represented in tableData ), the above code needs to subtract 1 from rowIndex .由于rowIndex还考虑了 header 行(未在tableData中表示),因此上述代码需要从rowIndex中减去 1。

You have some other errors that need to be corrected for the above to work:您还有一些其他错误需要更正才能使上述工作正常进行:

  • tableData should be a global variable so it is accessible in the different handlers tableData应该是一个全局变量,因此它可以在不同的处理程序中访问

  • tableData should then only get the new row added to it.然后tableData应该只将新行添加到其中。

  • The submit click handler should not be defined in the other handler, but outside of it.提交单击处理程序不应在其他处理程序中定义,而应在其外部定义。

Here is the corrected code:这是更正后的代码:

//declare the array as a global
var tableData = [];
$(document).ready(function () {
  $(".button").on("click", function () {
    para = document.getElementById("Parameter").value;
    condi = document.getElementById("Condition").value;
    value2match = document.getElementById("valuetomatch").value;

    if (para && condi && value2match !== null) {
      var table = document.getElementById("myTable");
      var row = table.insertRow(-1);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      var cell3 = row.insertCell(2);
      var cell4 = row.insertCell(3);

      cell1.innerHTML = document.getElementById("Parameter").value;
      cell2.innerHTML = document.getElementById("Condition").value;
      cell3.innerHTML = document.getElementById("valuetomatch").value;
      cell4.innerHTML =
        '<button  class = "del_img "onClick="delSpec(this)"><img src="deleteimg.png" width="30" height="30"></button> </div>';

      var myTab = document.getElementById("myTable");

      // Only add the new row:
      tableData.push([
        document.getElementById("Parameter").value,
        document.getElementById("Condition").value,
        document.getElementById("valuetomatch").value
      ]);

      modal.style.display = "none";
    } else {
      alert("All the input box cannot be empty!");
    }
  });
  // Move click handler outside of the other handler
  $("#buttonSubmit").onclick = function () {
    alert(tableData);
  };
});

function delSpec(node) {
  let r = node.parentNode.parentNode;
  tableData.splice(r.rowIndex - 1, 1);
  r.parentNode.removeChild(r);
}

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

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