2014-01-09 79 views
2

我建設我的HTML表格如下編輯或通過單擊同一行中HREF鏈接刪除當前錶行

HTML

<table cellpadding="0" cellspacing="0" border="0" class="rqstLines" id="rqstLines" width="90%"> 
<thead> 
    <tr> 
     <th>Product Code</th> 
     <th>Order Qty</th> 
     <th>Edit</th> 
     <th>Delete</th> 
    </tr> 
</thead> 
<tbody> 
</tbody> 
</table> 

的JavaScript

var ProductCode = $("#ProductCode option:selected").val(); 
    var productcode = $("#ProductCode option:selected").text(); 
    var qty = document.getElementById("OrderQty").value; 

    var table=document.getElementById("rqstLines"); 
    var row=table.insertRow(-1); 
    row.id = "tr-"+tblcounter; 
    var cell1=row.insertCell(0); 
    var cell2=row.insertCell(1); 
    var cell3=row.insertCell(2); 
    var cell4=row.insertCell(3); 

    cell1.innerHTML=productcode; 
    cell2.innerHTML=qty; 
    cell3.innerHTML='<a href="#" onClick="editTable();">Edit</a>'; 
    cell4.innerHTML='<a href="#" onClick="deleteRow();">Delete</a>'; 

    tblcounter++; 

這裏是我的deleteRow( )

function deleteRow() { 
    $(this).closest('tr').remove(); 
} 

但我無法刪除的行,甚至當我試圖從當前錶行的TD文本,它返回爲空,見下文

alert($(this).parent().siblings(':eq(0)').html()); 

這裏有什麼問題???

+0

你可以設置一個小提琴嗎? – Shashank

回答

1

改變你的代碼

 cell4.innerHTML='<a href="#" onClick="deleteRow();">Delete</a>'; 

 cell4.innerHTML='<a href="#" onClick="deleteRow(this);">Delete</a>'; 


    function deleteRow(obj) { 
    $(obj).closest('tr').remove(); 
} 
+0

謝謝Rajesh它的工作 – vamsi

0

嘗試:

function deleteRow() { 
    $(this).parents('tr').remove(); 
} 
+1

$(this).parent()將是​​,而不是 – vamsi

1

this您的功能是window而不是被點擊的元素。你需要通過參考:

cell4.innerHTML='<a href="#" onClick="deleteRow(this);">Delete</a>'; 

function deleteRow(el) { 
    $(el).closest('tr').remove(); 
} 
+0

謝謝Rory McCrossan – vamsi

相關問題