admin管理员组文章数量:1391960
I have a table that lists a bunch of customers. The last cell is an ajax delete button.
I want the row containing the deleted customer to be deleted through jQuery.
<table>
<tr><td>Customer info 1</td><td><a href="javascript:deleteCustomer(1);">Delete</a></td>
<tr><td>Customer info 2</td><td><a href="javascript:deleteCustomer(2);">Delete</a></td>
function deleteCustomer(customerId)
{
$.post("somepage.php", {cid:customerId}, function(data){
//...delete the row that called the function...
}
I have a table that lists a bunch of customers. The last cell is an ajax delete button.
I want the row containing the deleted customer to be deleted through jQuery.
<table>
<tr><td>Customer info 1</td><td><a href="javascript:deleteCustomer(1);">Delete</a></td>
<tr><td>Customer info 2</td><td><a href="javascript:deleteCustomer(2);">Delete</a></td>
function deleteCustomer(customerId)
{
$.post("somepage.php", {cid:customerId}, function(data){
//...delete the row that called the function...
}
Share
Improve this question
asked May 31, 2010 at 19:43
OMGKurtNilsenOMGKurtNilsen
5,0987 gold badges29 silver badges36 bronze badges
3 Answers
Reset to default 3Deleting is very relative...
All you really want is the user to not see it anymore. If you select the entire row, you can call toggle() on it, to make it hidden to the user. On the next page load it won't be there anyway (assuming your ajax request deleted it alright), so as long as it is hidden, it is as good as deleted...
Many ways to get back to the row. You can give it a unique ID of customerRow + the id.. so you have rows..
<tr id="customerRow1"> ... </tr>
<tr id="customerRow2"> ... </tr>
Then just call toggle on the element with the id customerRow + customerId
$("#customerRow" + customerId).toggle()
You could also add this as a parameter:
<table>
<tr><td>Customer info 1</td><td><a href="javascript:deleteCustomer(1,this);">Delete</a></td>
<tr><td>Customer info 2</td><td><a href="javascript:deleteCustomer(2,this);">Delete</a></td>
Your function would bee:
function deleteCustomer(customerId,element) {
$.post("somepage.php", {cid:customerId}, function(data){
$(element).closest("tr").toggle()
});
}
You could add this as a parameter:
<table>
<tr><td>Customer info 1</td><td><a href="javascript:deleteCustomer(1,this);">Delete</a></td>
<tr><td>Customer info 2</td><td><a href="javascript:deleteCustomer(2,this);">Delete</a></td>
Your function would bee:
function deleteCustomer(customerId,element) {
$.post("somepage.php", {cid:customerId}, function(data){
$(element).closest("tr").remove();
});
}
Another approach is to assign an id to each row:
<table>
<tr id='row_1'><td>Customer info 1</td><td><a href="javascript:deleteCustomer(1);">Delete</a></td>
<tr id='row_2'><td>Customer info 2</td><td><a href="javascript:deleteCustomer(2);">Delete</a></td>
Then you delete the row like this:
$("tr[id=row_" + customerId + "]").remove();
I have written an article on the same topic, you can check it out here:
Deleting Table Rows Using JQuery and PHP
本文标签: javascriptDelete current row in a lttablegt with jQueryStack Overflow
版权声明:本文标题:javascript - Delete current row in a <table> with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744601409a2615089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论