与使用 jquery 在此表中追加相反

发布于 2024-11-30 04:17:13 字数 850 浏览 0 评论 0原文

我需要与这段代码相反的内容:

if( $('#effort_<%= @project_task.id %>').length == 0 )
  $('#task_list').append('<tr><td><%= @project_task.project.project_number %> <%= @project_task.project.project_name %> - <%= @project_task.task_name %></td>' +
                         '<td><%= text_field :effort, :hours, :name => 'effort_' + @project_task.id.to_s, :id => 'effort_' + @project_task.id.to_s %></td>' +
                         '<td><%= link_to image_tag('icons/delete.png'), :method => :destroy, :remote => true %></td></tr>' );
  $('#effort_<%= @project_task.id.to_s %>').change( function() { submiteffort( this, <%= @project_task.id.to_s %>); } );

当用户单击删除按钮时,它需要删除表的该行。 我知道你可以使用 .remove() 但我不知道如何使用它,因为我是 RoR 新手。

I need the opposite to this peice of code:

if( $('#effort_<%= @project_task.id %>').length == 0 )
  $('#task_list').append('<tr><td><%= @project_task.project.project_number %> <%= @project_task.project.project_name %> - <%= @project_task.task_name %></td>' +
                         '<td><%= text_field :effort, :hours, :name => 'effort_' + @project_task.id.to_s, :id => 'effort_' + @project_task.id.to_s %></td>' +
                         '<td><%= link_to image_tag('icons/delete.png'), :method => :destroy, :remote => true %></td></tr>' );
  $('#effort_<%= @project_task.id.to_s %>').change( function() { submiteffort( this, <%= @project_task.id.to_s %>); } );

when the user clicks the delete button it needs to delete that row of the table.
I know you can use .remove() but im not sure how to use this since im new to RoR.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

菩提树下叶撕阳。 2024-12-07 04:17:13

您可以执行以下操作:

$("#task_list a").live("click", function(event) {
  $(event.target).closest("tr").remove();
});

确保使用 closestparents 将返回所有祖先 tr,因此如果您有嵌套表,它可能会删除超出所需数量的节点。 最近是最安全的选择。

You can do something like this:

$("#task_list a").live("click", function(event) {
  $(event.target).closest("tr").remove();
});

Make sure you use closest. parents will return all the ancestor trs so it might remove more nodes than needed if you have nested tables. closest is the safest option.

∞觅青森が 2024-12-07 04:17:13

假设您的删除位于每一行的 tr 内,

如果关闭按钮/图像具有类 close,则您需要执行类似的操作,然后像这样在其上绑定一个事件

$('close').live("click",function()
{
    $(this).parents("tr").remove();   //this here is referring to the button 
});

assuming your delete is inside the tr for each of the rows you need to do something like this

if the close buttton/image has a class close then bind an event on it like this

$('close').live("click",function()
{
    $(this).parents("tr").remove();   //this here is referring to the button 
});
月隐月明月朦胧 2024-12-07 04:17:13

这是您想要如何做的一个基本示例。我希望你不介意,但我已经把它精简到只剩下骨头了,但它应该能让你很好地知道从哪里开始。

JSFiddle:
http://jsfiddle.net/saceh/1/

<button id="Add">Add Row</button>
<hr>
<table id="TaskList">
</table>

<script>
// RowId is not important, just used to show you that rows are dynamically added
var RowId = 0;

$("#Add").click(function (e) {
    $("#TaskList").append("<tr><td>" + RowId  + "</td><td><button id='Delete'>Delete</button></td></tr>");
    RowId++;
});

$("#Delete").click(function(e) {
   var Row = $(this).parents('tr');
   $(Row).remove(); 
});

单击“删除”时,它会找到父行并将其删除,“添加”会执行与您目前正在执行的操作类似的操作。

Here is a basic example of how you want to do. I hope you dont mind but I've stripped it down to bare bones but it should give you a good idea of where to begin.

JSFiddle:
http://jsfiddle.net/saceh/1/

<button id="Add">Add Row</button>
<hr>
<table id="TaskList">
</table>

<script>
// RowId is not important, just used to show you that rows are dynamically added
var RowId = 0;

$("#Add").click(function (e) {
    $("#TaskList").append("<tr><td>" + RowId  + "</td><td><button id='Delete'>Delete</button></td></tr>");
    RowId++;
});

$("#Delete").click(function(e) {
   var Row = $(this).parents('tr');
   $(Row).remove(); 
});

When delete is clicked it finds the parent row and removes it and Add does something similar to what you are doing at the moment.

撑一把青伞 2024-12-07 04:17:13

您可以使用 parent 方法(添加一些标识符)指向要删除的行,然后使用remove 方法来实际删除它。

You can use parent method (add some identifiers) to point to the row you want to delete and than use the remove method to actually remove it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文