尝试使用Jquery基于单元格值删除表中的行

时间:2021-03-11 22:16:41

I am trying to delete a row in a table in jquery. each row has a check box as its first cell. On clicking delete button , it should delete only rows whose checkbox is checked. Following is the code I am trying

我试图在jquery中删除表中的行。每行都有一个复选框作为其第一个单元格。单击“删除”按钮时,应仅删除其复选框已选中的行。以下是我正在尝试的代码

function deleteRow(tableID) {
        var tableData = document.getElementById(tableID);
        var tabId = '#' + tableID + ' tr';
        var rowcount = $(tabId).length;         
        for ( var i = 0; i < rowcount; i++) {
            var chkbox = $(tableIDAct).children('td').eq(0).is(':checked');
                if (null != chkbox && true == chkbox) {
                    tableData.deleteRow(i);
                    rowcount--;
                    i--;
                }
        }
    }

Here I am not been able to iterate table by rows. Suggest me Thanks

在这里,我无法逐行迭代。建议我谢谢

2 个解决方案

#1


1  

probably the problem is that you are checking is(':checked') on td objects instead of inputs

可能问题是你在td对象上检查是(':checked')而不是输入

var chkbox = $(tabId).eq(i)
    .children('td').eq(0)
    .children('input[type=checkbox]')
    .is(':checked');

http://jsfiddle.net/9fr2x93k/

#2


1  

I think you have use for this

我想你已经习惯了

document.getElementById("myTable").deleteRow(0);

you can delete one by one

你可以逐个删除

   <table id="myTable">
      <tr>
        <td>cell 1</td>
        <td>cell 2</td>
      </tr>
      <tr>
        <td>cell 3</td>
        <td>cell 4</td>
      </tr>
    </table>
    <br> 

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction() {
        document.getElementById("myTable").deleteRow(0);
    }
    </script>

If more details then

如果有更多细节

http://mrbool.com/how-to-add-edit-and-delete-rows-of-a-html-table-with-jquery/26721

#1


1  

probably the problem is that you are checking is(':checked') on td objects instead of inputs

可能问题是你在td对象上检查是(':checked')而不是输入

var chkbox = $(tabId).eq(i)
    .children('td').eq(0)
    .children('input[type=checkbox]')
    .is(':checked');

http://jsfiddle.net/9fr2x93k/

#2


1  

I think you have use for this

我想你已经习惯了

document.getElementById("myTable").deleteRow(0);

you can delete one by one

你可以逐个删除

   <table id="myTable">
      <tr>
        <td>cell 1</td>
        <td>cell 2</td>
      </tr>
      <tr>
        <td>cell 3</td>
        <td>cell 4</td>
      </tr>
    </table>
    <br> 

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction() {
        document.getElementById("myTable").deleteRow(0);
    }
    </script>

If more details then

如果有更多细节

http://mrbool.com/how-to-add-edit-and-delete-rows-of-a-html-table-with-jquery/26721