0
jQuery('.chk').click(function(){
   for(var k=0; k<2; k++) {
      deleteRow(k);  
   }                       
});

和函数 deleteRow()

function deleteRow(id) {
    var table = document.getElementById("modem_list");
    var rowName = 'row_'+id;
    if (table.rows.namedItem(rowName)) {
        table.deleteRow((table.rows.namedItem(rowName).rowIndex));
    }
}

<table id="modem_list">
   <tr name="row_1">Test 1</tr>
   <tr name="row_2">Test 2</tr>
</table>
<a class="chk" href="">CLICK</a>

当我点击结果不是删除 2 个标签时,如何修复它?

4

3 回答 3

1
$(function(){

    $('a.chk').click(function(e){
        e.preventDefault();
        $('table#modem_list tr').remove();
    });
});
于 2012-06-13T16:35:22.477 回答
1

您的整个解决方案可能很短:

$('.chk').click(function() {
    $('#modem_list tr').each(function() {
        $(this).remove();
    });
});

(从我可以从您的问题中阅读/想象的内容来看。)

或者,要仅删除TR以您开头的类,row_可以使用:

$('.chk').click(function() {
    $('#modem_list tr[class^=row_]').each(function() {
        $(this).remove();
    });
});

较短的变体,没有循环遍历行,也可以工作——但我不确定:

$('.chk').click(function() {
    $('#modem_list tr[class^=row_]').remove();
});
于 2012-06-13T16:22:30.633 回答
0
You are starting the for loop from 0 and you row id is starting from 1 thats why second one is not removing 
jQuery('.chk').click(function(){
    for(var k=1; k<=2; k++) {
         deleteRow(k);  
    }                       
});

    //  OR you can use another solution
jQuery('.chk').click(function($) {
    $('#modem_list tr').each(function() {
        $(this).remove();
    });
于 2012-06-13T16:38:36.800 回答