[英]Removing a table row with jQuery in rails using a helper
這應該很容易,因為很多人都在問,但是答案似乎對我沒有用。
我有一個表,其中一行是用AJAX添加的。 我現在想用AJAX刪除。
我添加了這個部分:
<tr >
<td><%= f.hidden_field :project_id, :value => @project.id ,:disabled => true %></td>
<td><%= f.select :taskType, ['Pre-Sales','Project','Support','Fault Fixing','Out Of Hours'] %></td>
<td><%= f.text_field :task_name %></td>
<td><%= link_to_remove_fields "remove", f %></td>
</tr>
這導致了這個助手:
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end
這導致這個js:
function remove_fields(link) {
$(link).prev("input[type=hidden]").value = "1";
$(link).parent(".fields").hide();
//$(link).find('tr').remove();
$(link).remove();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
我可以刪除帶有刪除按鈕的列,但不能刪除行。 我認為我這樣做的方式不允許刪除'tr`。
我敢肯定這很簡單,只是我缺乏對jQuery的經驗而使我失望
更新渲染的HTML
<td>
<input id="project_project_tasks_attributes_1315581090843__destroy" type="hidden" value="false" name="project[project_tasks_attributes][1315581090843][_destroy]">
<a onclick="remove_fields(this); return false;" href="#">remove</a>
更改
//$(link).find('tr').remove();
至
$(link).parents('tr').remove();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.