使用javascript从表中动态删除行

时间:2016-05-30 08:04:10

标签: javascript html

在点击删除行按钮时,我需要从表格底部逐一删除行。

问题:

我已经使用javascript函数从我的表中删除了一行,而不是从底部逐个删除该行,除了前两行之外它将被删除。当单击删除按钮时,我需要逐个删除除了前两行之外的行。

这是我的代码

<html>
<head>
<script type="text/javascript">

        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;

                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }

        function deleteRow(tableID) {

            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=1; i<rowCount; i++) {
                var row = table.rows[i];
                    if(rowCount <= 2) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            catch(e) {
                alert(e);
            }
        }
    </script>

</head>

<body>
<form>

      <input type="button" value="Add Row" onclick="addRow('dataTable')" />

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

    <br/>
    <br/>

     <table id="dataTable" align="center" width="350px" border="1">

   <tr>
         <th> Product Name</th>
          <th>Quantity</th>
         <th> Brand</th>       

    </tr>

    <tr>

   <td> <input type="text" name="pname"/></td> &nbsp;
   <td><input type="text" name="qty"/></td>
    <td><select name="brand"/>
    <option value="select">SELECT</option>
  </select>
    </td>



  </table>
</form>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

这有助于你:

function deleteRow(tableID) {

    try {

       var table = document.getElementById(tableID);

       var rowDelete = table.rows.length - 1;

       if (rowDelete > 1)

           table.deleteRow(rowDelete);

       else

         alert("Cannot delete all the rows.")
    }

    catch(e) {

        alert(e);
    }
}

最终代码:

<html>
<head>
<script type="text/javascript">

        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[1].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;

                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }

       function deleteRow(tableID) {

        try {

           var table = document.getElementById(tableID);

           var rowDelete = table.rows.length - 1;

           if (rowDelete > 1)

               table.deleteRow(rowDelete);

           else

             alert("Cannot delete all the rows.")
        }

        catch(e) {

            alert(e);
        }
    }
    </script>

</head>

<body>
<form>

      <input type="button" value="Add Row" onclick="addRow('dataTable')" />

    <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />

    <br/>
    <br/>

     <table id="dataTable" align="center" width="350px" border="1">

   <tr>
         <th> Product Name</th>
          <th>Quantity</th>
         <th> Brand</th>       

    </tr>

    <tr>

   <td> <input type="text" name="pname" value="" /></td> &nbsp;
   <td><input type="text" name="qty" value=""/></td>
    <td><select name="brand"/>
        <select>
           <option value="select">SELECT</option>

       </select>
    </td>
  </table>
</form>
</body>
</html>

答案 1 :(得分:0)

在进入循环删除行之前,应首先检查行数。或者你可以删除循环,因为你只删除了一行,这是最后一行(rowCount-1);

试试这个:

function deleteRow(tableID) {

            try {
                    var table = document.getElementById(tableID);
                    var rowCount = table.rows.length;

                    if (rowCount > 2){

                        //for(var i =1; i < 2; i++) {
                        //  var row = table.rows[i];
                              table.deleteRow(rowCount-1);

                         //}
                    }
                    else{
                     alert("Cannot delete all the rows.");
                    }

             }
             catch(e) {
                alert(e);
             }
}

答案 2 :(得分:0)

这是工作代码,你没有在删除一行后中断执行,这就是它删除所有行的原因

function deleteRow(tableID) {

            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=1; i<rowCount; i++) {
                var row = table.rows[i];
                    if(rowCount <= 2) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    else{
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                        break;
                    }
                }


            }
            catch(e) {
                alert(e);
            }
        }

答案 3 :(得分:0)

看看这是否有帮助:

$('#tbl1').on('click', 'input[type="button"]', function () {
    $(this).closest('tr').remove();
})

$('p input[type="button"]').click(function () {
    $('#tbl1').append('<tr><td><input type="text" class="txtBox" /></td><td><input type="button" value="X" /></td></tr>')
});

这可以使用HTML代码段

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="tbl1" style="border: 1px solid blue">
    <tr>
        <td>
            <input type="text" class="txtBox" />
        </td>
        <td>
            <input type="button" value="X" />
        </td>
    </tr>
</table>
<p>
    <input type="button" value="Add">
</p>