【问题标题】:Add/Delete table rows dynamically using JavaScript使用 JavaScript 动态添加/删除表行
【发布时间】:2019-02-22 15:48:32
【问题描述】:

我正在尝试添加/删除this example this example 之后的表行。

这是我的代码:

HTML 表单

<div id="POItablediv">
    <input type="button" id="addPOIbutton" value="Add POIs"/><br/><br/>
    <table id="POITable" border="1">
        <tr>
            <td>POI</td>
            <td>Latitude</td>
            <td>Longitude</td>
            <td>Delete?</td>
            <td>Add Rows?</td>
        </tr>
        <tr>
            <td>1</td>
            <td><input size=25 type="text" id="latbox" readonly=true/></td>
            <td><input size=25 type="text" id="lngbox" readonly=true/></td>
            <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>
            <td><input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/></td>
        </tr>
    </table>
</div>

JavaScript

function deleteRow(row)
{
    var i=row.parentNode.parentNode.rowIndex;
    document.getElementById('POITable').deleteRow(i);
}


function insRow()
{
    var x=document.getElementById('POITable').insertRow(1);
    var c1=x.insertCell(0);
    var c2=x.insertCell(1);
    c1.innerHTML="NEW CELL1";
    c2.innerHTML="NEW CELL2";
}

现在,如您所见,在我的表格中,我有文本字段和按钮。我想要什么:

  1. 只是为了重复行的结构。我现在不能这样做,因为 innerHTM 只接收文本。如何插入文本字段或标签?

  2. 文本字段的 id 也应该不同,因为我稍后会检索值并将其放入数据库中。

  3. 我也想添加一个函数来增加 POI 的数量

谁能帮帮我?

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您可以只克隆具有输入的第一行,然后获取嵌套输入并更新其 ID 以添加行号(并对第一个单元格执行相同操作)。

    function deleteRow(row)
    {
        var i=row.parentNode.parentNode.rowIndex;
        document.getElementById('POITable').deleteRow(i);
    }
    
    
    function insRow()
    {
        var x=document.getElementById('POITable');
           // deep clone the targeted row
        var new_row = x.rows[1].cloneNode(true);
           // get the total number of rows
        var len = x.rows.length;
           // set the innerHTML of the first row 
        new_row.cells[0].innerHTML = len;
    
           // grab the input from the first cell and update its ID and value
        var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
        inp1.id += len;
        inp1.value = '';
    
           // grab the input from the first cell and update its ID and value
        var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
        inp2.id += len;
        inp2.value = '';
    
           // append the new row to the table
        x.appendChild( new_row );
    }
    

    下面的演示

    function deleteRow(row) {
      var i = row.parentNode.parentNode.rowIndex;
      document.getElementById('POITable').deleteRow(i);
    }
    
    
    function insRow() {
      console.log('hi');
      var x = document.getElementById('POITable');
      var new_row = x.rows[1].cloneNode(true);
      var len = x.rows.length;
      new_row.cells[0].innerHTML = len;
    
      var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
      inp1.id += len;
      inp1.value = '';
      var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
      inp2.id += len;
      inp2.value = '';
      x.appendChild(new_row);
    }
    <div id="POItablediv">
      <input type="button" id="addPOIbutton" value="Add POIs" /><br/><br/>
      <table id="POITable" border="1">
        <tr>
          <td>POI</td>
          <td>Latitude</td>
          <td>Longitude</td>
          <td>Delete?</td>
          <td>Add Rows?</td>
        </tr>
        <tr>
          <td>1</td>
          <td><input size=25 type="text" id="latbox" /></td>
          <td><input size=25 type="text" id="lngbox" readonly=true/></td>
          <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)" /></td>
          <td><input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()" /></td>
        </tr>
      </table>

    【讨论】:

    • 非常感谢! .. 工作就像小菜一碟!
    • @Pow:不客气。我把another example放在一起。这个将标题行放在&lt;thead&gt; 元素中,其余放在&lt;tbody&gt; 元素中。此外,我更新了代码以在删除行时更新行号和 ID,并将一些代码移到它自己的函数中。
    • 我还有一个问题。如果我没有任何已创建的行怎么办?假设我想做完全相同的事情。但是第 2 行(正在克隆的)最初并不存在。
    • @Pow:使用我上面评论中的示例,将生成初始克隆的行从第一行更改为:clone = tbody.removeChild(tbody.rows[0]);。因此,您将从服务器发送该行,并立即将其删除,并将其存储在clone 中。然后创建另一个按钮来添加该初始行。无论如何,您可能只想在表格外有一个按钮来添加行,因为没有太多理由让同一个按钮在表格中一遍又一遍地重复。
    • 谢谢...我试试看!
    【解决方案2】:

    简单的 Javascript 添加更多具有删除功能的行

    干杯!

    <TABLE id="dataTable">
    <tr><td>
                   <INPUT TYPE=submit name=submit id=button class=btn_medium VALUE=\'Save\'  >
                   <INPUT type="button" value="AddMore" onclick="addRow(\'dataTable\')" class="btn_medium"    /> 
    </td></tr>
    
    <TR>
    <TD> 
                    <input type="text" size="20" name="values[]"/> <br><small><font color="gray">Enter Title</font></small> 
    </TD>
    </TR>
    </table>
    
    
    
    <script>
    
    
    function addRow(tableID) {
    
            var table = document.getElementById(tableID);
    
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
    
            var cell3 = row.insertCell(0);
           cell3.innerHTML = cell3.innerHTML +' <input type="text" size="20" name="values[]"/> <INPUT type="button"  class="btn_medium" value="Remove" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);" /><br><small><font color="gray">Enter Title</font></small>';
      //cell3.innerHTML = cell3.innerHTML +' <input type="text" size="20" name="values[]"/> <INPUT type="button"  class="btn_medium" value="Remove" onclick="this.parentNode.parentNode.innerHTML=\'\';" /><br><small><font color="gray">Enter Title</font></small>';       
    }
    
    </script>
    

    【讨论】:

      【解决方案3】:

      这似乎比上面的答案干净得多......

      <script>
      var maxID = 0;
      function getTemplateRow() {
      var x = document.getElementById("templateRow").cloneNode(true);
      x.id = "";
      x.style.display = "";
      x.innerHTML = x.innerHTML.replace(/{id}/, ++maxID);
      return x;
      }
      function addRow() {
      var t = document.getElementById("theTable");
      var rows = t.getElementsByTagName("tr");
      var r = rows[rows.length - 1];
      r.parentNode.insertBefore(getTemplateRow(), r);
      
      }
      </script>
      
      
      <table id="theTable">
      <tr>
      <td>id</td>
      <td>name</td>
      </tr>
      <tr id="templateRow" style="display:none">
      <td>{id}</td>
      <td><input /></td>
      </tr>
      </table>
      
      
      <button onclick="addRow();">Go</button>
      

      【讨论】:

      • 谢谢...是的,我猜是的 :)
      • @BradLaney 为什么要为这样的事情学习 jQuery?我并不反对 jQuery,但是,很多人依赖 jQuery 来完成可以使用纯 JS 完成的简单事情。几年来,人们甚至会忘记 JS 的基础知识。
      • 对于原型设计,没有比这更好的了。
      【解决方案4】:

      如果你在每一行都放一个删除按钮,那么:

      <tr>
        <td><input type="button" value="Delete row" onclick="deleteRow(this);">
        <td><input type="text">
        <td><input type="text">
      

      而del​​eteRow函数可以是:

      function deleteRow(el) {
          // while there are parents, keep going until reach TR 
          while (el.parentNode && el.tagName.toLowerCase() != 'tr') {
              el = el.parentNode;
          }
      
          // If el has a parentNode it must be a TR, so delete it
          // Don't delte if only 3 rows left in table
          if (el.parentNode && el.parentNode.rows.length > 3) {
              el.parentNode.removeChild(el);
          }
      }
      

      如果您的所有行都具有相同的内容,则通过克隆现有行来添加行会快得多:

      function addRow(tableID) {
          var table = document.getElementById(tableID);
      
          if (!table) return;
      
          var newRow = table.rows[1].cloneNode(true);
      
          // Now get the inputs and modify their names 
          var inputs = newRow.getElementsByTagName('input');
      
          for (var i=0, iLen=inputs.length; i<iLen; i++) {
              // Update inputs[i]
          }
      
          // Add the new row to the tBody (required for IE)
          var tBody = table.tBodies[0];
          tBody.insertBefore(newRow, tBody.lastChild);
      }
      

      【讨论】:

      • 这会在最后一行之前添加新行。如何在最后一行之后添加新行?
      【解决方案5】:

      您可以像这样以最简单的方式向表中添加一行:-

      我发现这是添加 row 的最简单方法。令人敬畏的是,即使它包含输入元素,它也不会更改已经存在的表格内容。

      row = `<tr><td><input type="text"></td></tr>`
      $("#table_body tr:last").after(row) ;
      

      这里#table_body是表体标签的id。

      【讨论】:

        【解决方案6】:

        1 & 2:innerHTML 可以接受 HTML 和文本。你可以这样做:

        c1.innerHTML = "&lt;input size=25 type=\"text\" id='newID' readonly=true/&gt;";

        可能是也可能不是最好的方法,但你可以这样做。

        3:我只会使用一个全局变量来保存 POI 的数量并每次递增/递减它。

        【讨论】:

          【解决方案7】:

          这是包含 HTML、CSS 和 JS 的完整代码。

          <style><style id='generate-style-inline-css' type='text/css'>
              body {
                  background-color: #efefef;
                  color: #3a3a3a;
              }
          
              a,
              a:visited {
                  color: #1e73be;
              }
          
              a:hover,
              a:focus,
              a:active {
                  color: #000000;
              }
          
              body .grid-container {
                  max-width: 1200px;
              }
          
              body,
              button,
              input,
              select,
              textarea {
                  font-family: "Open Sans", sans-serif;
              }
          
              .entry-content>[class*="wp-block-"]:not(:last-child) {
                  margin-bottom: 1.5em;
              }
          
              .main-navigation .main-nav ul ul li a {
                  font-size: 14px;
              }
          
              @media (max-width:768px) {
                  .main-title {
                      font-size: 30px;
                  }
                  h1 {
                      font-size: 30px;
                  }
                  h2 {
                      font-size: 25px;
                  }
              }
          
              .top-bar {
                  background-color: #636363;
                  color: #ffffff;
              }
          
              .top-bar a,
              .top-bar a:visited {
                  color: #ffffff;
              }
          
              .top-bar a:hover {
                  color: #303030;
              }
          
              .site-header {
                  background-color: #ffffff;
                  color: #3a3a3a;
              }
          
              .site-header a,
              .site-header a:visited {
                  color: #3a3a3a;
              }
          
              .main-title a,
              .main-title a:hover,
              .main-title a:visited {
                  color: #222222;
              }
          
              .site-description {
                  color: #757575;
              }
          
              .main-navigation,
              .main-navigation ul ul {
                  background-color: #222222;
              }
          
              .main-navigation .main-nav ul li a,
              .menu-toggle {
                  color: #ffffff;
              }
          
              .main-navigation .main-nav ul li:hover>a,
              .main-navigation .main-nav ul li:focus>a,
              .main-navigation .main-nav ul li.sfHover>a {
                  color: #ffffff;
                  background-color: #3f3f3f;
              }
          
              button.menu-toggle:hover,
              button.menu-toggle:focus,
              .main-navigation .mobile-bar-items a,
              .main-navigation .mobile-bar-items a:hover,
              .main-navigation .mobile-bar-items a:focus {
                  color: #ffffff;
              }
          
              .main-navigation .main-nav ul li[class*="current-menu-"]>a {
                  color: #ffffff;
                  background-color: #3f3f3f;
              }
          
              .main-navigation .main-nav ul li[class*="current-menu-"]>a:hover,
              .main-navigation .main-nav ul li[class*="current-menu-"] .sfHover>a {
                  color: #ffffff;
                  background-color: #3f3f3f;
              }
          
              .navigation-search input[type="search"],
              .navigation-search input[type="search"]:active {
                  color: #3f3f3f;
                  background-color: #3f3f3f;
              }
          
              .navigation-search input[type="search"]:focus {
                  color: #ffffff;
                  background-color: #3f3f3f;
              }
          
              .main-navigation ul ul {
                  background-color: #3f3f3f;
              }
          
              .main-navigation .main-nav ul ul li a {
                  color: #ffffff;
              }
          
              .main-navigation .main-nav ul ul li:hover>a,
              .main-navigation .main-nav ul ul li:focus>a,
              .main-navigation .main-nav ul ul li.sfHover>a {
                  color: #ffffff;
                  background-color: #4f4f4f;
              }
          
              .main-navigation . main-nav ul ul li[class*="current-menu-"]>a {
                  color: #ffffff;
                  background-color: #4f4f4f;
              }
          
              .main-navigation .main-nav ul ul li[class*="current-menu-"]>a:hover,
              .main-navigation .main-nav ul ul li[class*="current-menu-"] .sfHover>a {
                  color: #ffffff;
                  background-color: #4f4f4f;
              }
          
              .separate-containers .inside-article,
              .separate-containers .comments-area,
              .separate-containers .page-header,
              .one-container .container,
              .separate-containers .paging-navigation,
              .inside-page-header {
                  background-color: #ffffff;
              }
          
              .entry-meta {
                  color: #595959;
              }
          
              .entry-meta a,
              .entry-meta a:visited {
                  color: #595959;
              }
          
              .entry-meta a:hover {
                  color: #1e73be;
              }
          
              .sidebar .widget {
                  background-color: #ffffff;
              }
          
              .sidebar .widget .widget-title {
                  color: #000000;
              }
          
              .footer-widgets {
                  background-color: #ffffff;
              }
          
              .footer-widgets .widget-title {
                  color: #000000;
              }
          
              .site-info {
                  color: #ffffff;
                  background-color: #222222;
              }
          
              .site-info a,
              .site-info a:visited {
                  color: #ffffff;
              }
          
              .site-info a:hover {
                  color: #606060;
              }
          
              .footer-bar .widget_nav_menu .current-menu-item a {
                  color: #606060;
              }
          
              input[type="text"],
              input[type="email"],
              input[type="url"],
              input[type="password"],
              input[type="search"],
              input[type="tel"],
              input[type="number"],
              textarea,
              select {
                  color: #666666;
                  background-color: #fafafa;
                  border-color: #cccccc;
              }
          
              input[type="text"]:focus,
              input[type="email"]:focus,
              input[type="url"]:focus,
              input[type="password"]:focus,
              input[type="search"]:focus,
              input[type="tel"]:focus,
              input[type="number"]:focus,
              textarea:focus,
              select:focus {
                  color: #666666;
                  background-color: #ffffff;
                  border-color: #bfbfbf;
              }
          
              button,
              html input[type="button"],
              input[type="reset"],
              input[type="submit"],
              a.button,
              a.button:visited,
              a.wp-block-button__link:not(.has-background) {
                  color: #ffffff;
                  background-color: #666666;
              }
          
              button:hover,
              html input[type="button"]:hover,
              input[type="reset"]:hover,
              input[type="submit"]:hover,
              a.button:hover,
              button:focus,
              html input[type="button"]:focus,
              input[type="reset"]:focus,
              input[type="submit"]:focus,
              a.button:focus,
              a.wp-block-button__link:not(.has-background):active,
              a.wp-block-button__link:not(.has-background):focus,
              a.wp-block-button__link:not(.has-background):hover {
                  color: #ffffff;
                  background-color: #3f3f3f;
              }
          
              .generate-back-to-top,
              .generate-back-to-top:visited {
                  background-color: rgba( 0, 0, 0, 0.4);
                  color: #ffffff;
              }
          
              .generate-back-to-top:hover,
              .generate-back-to-top:focus {
                  background-color: rgba( 0, 0, 0, 0.6);
                  color: #ffffff;
              }
          
              .entry-content .alignwide,
              body:not(.no-sidebar) .entry-content .alignfull {
                  margin-left: -40px;
                  width: calc(100% + 80px);
                  max-width: calc(100% + 80px);
              }
          
              @media (max-width:768px) {
                  .separate-containers .inside-article,
                  .separate-containers .comments-area,
                  .separate-containers .page-header,
                  .separate-containers .paging-navigation,
                  .one-container .site-content,
                  .inside-page-header {
                      padding: 30px;
                  }
                  .entry-content .alignwide,
                  body:not(.no-sidebar) .entry-content .alignfull {
                      margin-left: -30px;
                      width: calc(100% + 60px);
                      max-width: calc(100% + 60px);
                  }
              }
          
              .rtl .menu-item-has-children .dropdown-menu-toggle {
                  padding-left: 20px;
              }
          
              .rtl .main-navigation .main-nav ul li.menu-item-has-children>a {
                  padding-right: 20px;
              }
          
              .one-container .sidebar .widget {
                  padding: 0px;
              }
          
              .append_row {
                  color: black !important;
                  background-color: #FFD6D6 !important;
                  border: 1px #ccc solid !important;
              }
          
              .append_column {
                  color: black !important;
                  background-color: #D6FFD6 !important;
                  border: 1px #ccc solid !important;
              }
          
              table#my-table td {
                  width: 50px;
                  height: 27px;
                  border: 1px solid #D3D3D3;
                  text-align: center;
                  padding: 0;
              }
          
              div#my-container input {
                  padding: 5px;
                  font-size: 12px !important;
                  width: 100px;
                  margin: 2px;
              }
          
              .row {
                  background-color: #FFD6D6 !important;
              }
          
              .col {
                  background-color: #D6FFD6 !important;
              }
              </style>
              <script src="https://code.jquery.com/jquery-1.11.0.js"></script>
              <script>
          
              // append row to the HTML table
              function appendRow() {
                  var tbl = document.getElementById('my-table'), // table reference
                      row = tbl.insertRow(tbl.rows.length),      // append table row
                      i;
                  // insert table cells to the new row
                  for (i = 0; i < tbl.rows[0].cells.length; i++) {
                      createCell(row.insertCell(i), i, 'row');
                  }
              }
          
              // create DIV element and append to the table cell
              function createCell(cell, text, style) {
                  var div = document.createElement('div'), // create DIV element
                      txt = document.createTextNode(text); // create text node
                  div.appendChild(txt);                    // append text node to the DIV
                  div.setAttribute('class', style);        // set DIV class attribute
                  div.setAttribute('className', style);    // set DIV class attribute for IE (?!)
                  cell.appendChild(div);                   // append DIV to the table cell
              }
          
              // append column to the HTML table
              function appendColumn() {
                  var tbl = document.getElementById('my-table'), // table reference
                      i;
                  // open loop for each row and append cell
                  for (i = 0; i < tbl.rows.length; i++) {
                      createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), i, 'col');
                  }
              }
          
              // delete table rows with index greater then 0
              function deleteRows() {
                  var tbl = document.getElementById('my-table'), // table reference
                      lastRow = tbl.rows.length - 1,             // set the last row index
                      i;
                  // delete rows with index greater then 0
                  for (i = lastRow; i > 0; i--) {
                      tbl.deleteRow(i);
                  }
              }
          
              // delete table columns with index greater then 0
              function deleteColumns() {
                  var tbl = document.getElementById('my-table'), // table reference
                      lastCol = tbl.rows[0].cells.length - 1,    // set the last column index
                      i, j;
                  // delete cells with index greater then 0 (for each row)
                  for (i = 0; i < tbl.rows.length; i++) {
                      for (j = lastCol; j > 0; j--) {
                          tbl.rows[i].deleteCell(j);
                      }
                  }
              }
              </script>
              <div id="my-container">
              <center><br>
              <input type="button" value="Add row" onclick="javascript:appendRow()" class="append_row"><br>
              <input type="button" value="Add column" onclick="javascript:appendColumn()" class="append_column"><br>
              <input type="button" value="Delete rows" onclick="javascript:deleteRows()" class="delete"><br>
              <input type="button" value="Delete columns" onclick="javascript:deleteColumns()" class="delete"><br>
              <input type="button" value="Delete both" onclick="javascript:deleteColumns();deleteRows()" class="delete"><p></p>
              <table id="my-table" align="center" cellspacing="0" cellpadding="0" border="0">
              <tbody><tr>
              <td>Small</td>
              </tr>
              </tbody></table>
              <p></p></center>
              </div>
          

          【讨论】:

            【解决方案8】:

            此代码可能对某些人有所帮助

                <HTML>
            <HEAD>
                <TITLE> Add/Remove dynamic rows in HTML table </TITLE>
                <style type="text/css">
                    .democlass{
                        color:red;
                    }
                </style>
                <SCRIPT language="javascript">
                    function addRow(tableID) {
            
                        var table = document.getElementById(tableID);
                        var rowCount = table.rows.length;
                        var colCount = table.rows[0].cells.length;
            
                        var row = table.insertRow(rowCount);
                        for(var i = 0; i < colCount; i++) {
                            var newcell = row.insertCell(i);
                            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                        }
            
                        row = table.insertRow(table.rows.length);
                        for(var i = 0; i < colCount; i++) {
                            var newcell = row.insertCell(i);
                            newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                        }
            
                        row = table.insertRow(table.rows.length);
                        for(var i = 0; i < colCount; i++) {
                            var newcell = row.insertCell(i);
                            newcell.innerHTML = table.rows[2].cells[i].innerHTML;
                        }
            
                        row = table.insertRow(table.rows.length);
                        for(var i = 0; i < colCount; i++) {
            
                            var newcell = row.insertCell(i);
                            if(i == (colCount - 1)) {
                                newcell.innerHTML = "<INPUT type=\"button\" value=\"Delete Row\" onclick=\"removeRow(this)\"/>";
                            } else {
                                newcell.innerHTML = table.rows[3].cells[i].innerHTML;
                            }
                        }
                    }
            
                    /**
                     * This method deletes the specified section of the table
                     * OR deletes the specified rows from the table.
                     */
                    function removeRow(src) {
            
                        var oRow = src.parentElement.parentElement;
                        var rowsCount = 0;
                        for(var index = oRow.rowIndex; index >= 0; index--) {
            
                            document.getElementById("dataTable").deleteRow(index);
                            if(rowsCount == (4 - 1)) {
                                return;
                            }
                            rowsCount++;
                        }
                        //once the row reference is obtained, delete it passing in its rowIndex
                        /* document.getElementById("dataTable").deleteRow(oRow.rowIndex); */
                    }
                </SCRIPT>
            </HEAD>
            <BODY>
                <form name="myForm">
                    <TABLE id="dataTable" width="350px" border="1">
                        <TR>
                            <TD>
                                <INPUT type="checkbox" name="chk"/>
                            </TD>
                            <TD>
                                Code
                            </TD>
                            <TD>
                                <INPUT type="text" name="txt"/>
                            </TD>
                            <TD>
                                Select Country
                            </TD>
                            <TD>
                                <SELECT name="country">
                                    <OPTION value="in">India</OPTION>
                                    <OPTION value="de">Germany</OPTION>
                                    <OPTION value="fr">France</OPTION>
                                    <OPTION value="us">United States</OPTION>
                                    <OPTION value="ch">Switzerland</OPTION>
                                </SELECT>
                            </TD>
                        </TR>
                        <TR>
                            <TD>&nbsp;</TD>
                            <TD>
                                First Name
                            </TD>
                            <TD>
                                <INPUT type="text" name="txt1"/>
                            </TD>
                            <TD>
                                Last Name
                            </TD>
                            <TD>
                                <INPUT type="text" name="txt2"/>
                            </TD>
                        </TR>
                        <TR>
                            <TD>&nbsp;</TD>
                            <TD>Phone</TD>
                            <TD>
                                <INPUT type="text" name="txt3"/>
                            </TD>
                            <TD>Address</TD>
                            <TD>
                                <INPUT type="text" name="txt4" class="democlass"/>
                            </TD>
                        </TR>
                        <TR>
                            <TD>&nbsp;</TD>
                            <TD>&nbsp;</TD>
                            <TD>
                                &nbsp;
                            </TD>
                            <TD>&nbsp;</TD>
                            <TD>
                                <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
                            </TD>
                        </TR>
                    </TABLE>
            </BODY>
            </HTML>
            

            【讨论】:

              【解决方案9】:

              我使用了上面指出的一些解决方案以及其他帖子中的解决方案,为包含输入字段的动态表提出了一个可行的解决方案。我这样做是因为它可能会帮助在搜索导致我找到它的相同内容后找到该线程的人,并且还因为接受的答案(和相关的 jsfiddle)实际上不起作用!也就是说,在多次插入/删除后,它不会正确索引表行。关键问题是如何唯一索引动态行数据,这可以通过一点jquery来实现:

              <form id=frmLines>
                <table id=tabLines>
                <tr>
                  <td>img src='/some/suitable/graphic' onclick='removeLine(this);'/></td>
                  <td><input type='text' name='field1' /></td>
                  <td><input type='text' name='field2' /></td>
                  <td><input type='text' name='field3' /></td>
                </tr>
                <tr>
                  <td><img src='/some/suitable/graphic' onclick='addLine();' /></td>
                  <td colspan=3>&nbsp;</td>
                </tr>
                </table>
              </form>
              

              请注意,表单和表格具有用于直接 DOM 引用的 id,但您不能在输入字段上使用 id,因为要使它们独一无二,您需要引入一个索引,这会使代码变得非常复杂 - 而且它很容易在处理表单时按名称访问它们(见下文)

              那么控制添加和删除行的javascript是这样的:

              function addLine() {
                var tabLines = document.getElementById("tabLines");
                var tabLinesRow = tabLines.insertRow(tabLines.rows.length-1);
                var col1html = "<img src='/some/suitable/graphic' onclick='removeLine(this);'>";
                var col2html = "<input type='text' name='field1' />";
                var col3html = "<input type='text' name='field2' />";
                var col4html = "<input type='text' name='field3' />";
              
                var col1 = tabLinesRow.insertCell(0); col1.innerHTML=col1html;
                var col2 = tabLinesRow.insertCell(1); col2.innerHTML=col2html;
                var col3 = tabLinesRow.insertCell(2); col3.innerHTML=col3html;
                var col4 = tabLinesRow.insertCell(3); col4.innerHTML=col4html;
              }
              
              function removeLine(lineItem) {
                var row = lineItem.parentNode.parentNode;
                row.parentNode.removeChild(row);
              }
              

              然后是拼图的最后一部分 - 提交表单数据时处理表单数据的 javascript。这里的关键 jquery 函数是 .eq() - 它允许您按照它们在表单中出现的顺序访问字段名称 - 即按表行顺序。

              var frmData = {};              // an object to contain all form data
              var arrLines = new Array();    // array to contain the dynamic lines
              
              var tabLines = document.getElementById("tabLines").rows.length-1;
              
              for (i=0;i<tabLines;i++) {
                arrLines[i] = {};
                arrLines[i]['no']     = i+1;
                arrLines[i]['field1'] = $("#frmLines input[name=field1]").eq(i).val();
                arrLines[i]['field2'] = $("#frmLines input[name=field2]").eq(i).val();
                arrLines[i]['field3'] = $("#frmLines input[name=field3]").eq(i).val();
              }
              frmData['lines'] = arrLines;
              
              frmData['another_field'] = $('#frmLines input[name=another_field]").val();
              
              var jsonData = JSON.stringify(frmData);
              
              // lines of data now in a JSON structure as indexed array
              // (plus other fields in the JSON as required)
              // ready to post via ajax etc
              

              我希望这可以直接或间接地帮助某人。使用了一些微妙的技术,它们并不复杂,但我花了 3-4 个小时才拼凑起来。

              【讨论】:

                【解决方案10】:

                Javascript动态添加表格数据。

                脚本

                function addRow(tableID) {
                    var table = document.getElementById(tableID);
                    var rowCount = table.rows.length;
                    var colCount = table.rows[0].cells.length;    
                    var validate_Noof_columns = (colCount - 1); // •No Of Columns to be Validated on Text.
                    for(var j = 0; j < colCount; j++) { 
                        var text = window.document.getElementById('input'+j).value;
                
                        if (j == validate_Noof_columns) {
                            row = table.insertRow(2); // •location of new row.
                            for(var i = 0; i < colCount; i++) {       
                            var text = window.document.getElementById('input'+i).value;
                            var newcell = row.insertCell(i);
                                if(i == (colCount - 1)) {  // Replace last column with delete button
                    newcell.innerHTML = "<INPUT type='button' value='X' onclick='removeRow(this)'/>"; break;
                                } else  {
                                    newcell.innerHTML = text;
                                    window.document.getElementById('input'+i).value = '';
                                }
                            }   
                        }else if (text != 'undefined' && text.trim() == ''){ 
                            alert('input'+j+' is EMPTY');break;
                        }  
                    }   
                }
                function removeRow(onclickTAG) {
                    // Iterate till we find TR tag. 
                    while ( (onclickTAG = onclickTAG.parentElement)  && onclickTAG.tagName != 'TR' );
                            onclickTAG.parentElement.removeChild(onclickTAG);      
                }
                

                HTML

                <div align='center'>
                <TABLE id='dataTable' border='1' >
                <TBODY>
                    <TR><th align='center'><b>First Name:</b></th>
                        <th align='center' colspan='2'><b>Last  Name:</b></th>
                        <th></th>
                    </TR>    
                    <TR><TD ><INPUT id='input0' type="text"/></TD>              
                        <TD ><INPUT id='input1' type='text'/></TD>
                        <TD>
                <INPUT type='button' id='input2' value='+' onclick="addRow('dataTable')" />
                        </TD>
                    </TR>
                </TBODY> 
                </TABLE>
                </div>
                

                例如:jsfiddle

                【讨论】:

                  猜你喜欢
                  • 2021-08-28
                  • 2021-08-28
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-09-26
                  • 2017-04-05
                  • 2013-02-12
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多