2011-08-19 64 views
2

相反,我需要与此相反peice的代码:追加此表的使用jQuery

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。

+1

RoR输出html,所以你可以使用jquery normaly – Awea

回答

2

你可以做这样的事情:

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

请确保您使用closestparents将返回所有祖先tr s,因此如果您有嵌套表,它可能会删除比需要更多的节点。 closest是最安全的选择。

0

以下是您想要做的一个基本示例。我希望你不要介意,但我已经把它剥离到骨头,但它应该给你一个好主意从哪里开始。

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

删除时点击它找到父行并将其删除,并添加确实类似于你此刻在做什么东西。

0

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

1

假设你删除的是TR每个你需要做这样的事情

如果关闭按钮移到/图像有着密切然后在其上一个事件绑定这样

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