2012-06-13 18 views
0
jQuery('.chk').click(function(){ 
    for(var k=0; k<2; k++) { 
     deleteRow(k); 
    }      
}); 

的删除价值和功能deleteRow()错误当标签<tr>

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标签,如何解决呢?

+1

什么是'val'? –

+1

这与PHP无关。 – nickb

+0

您正在使用jQuery。不需要像在第二个代码块中那样使用原生DOM方法。 – ThiefMaster

回答

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(); 
}); 
1
$(function(){ 

    $('a.chk').click(function(e){ 
     e.preventDefault(); 
     $('table#modem_list tr').remove(); 
    }); 
}); 
+0

+1 for'e.preventDefault();';但为什么':空'? – feeela

+0

嗨,你是对的,但表格语法是错误的,所以dom中tr标签中的文本被移出表格。无论如何,我会纠正我的答案。谢谢 – Mouloud

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(); 
    });