2012-06-13 16 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(); 
    });