我有许多带有form_for元素的动态创建表单(,form_for)。我需要创建一个点击提交。当我单击复选框时,它会提交页面上的第一个表单,而不是当前的单击表单。如何提交当前的单击表单?
<%= form_for todo, url: update_todos_path, remote: true, html: { id: 'todo_status'} do |t| %>
<%= t.text_field :id %>
<%= t.check_box :isCompleted, {}, 'false', 'true' %>
<div class='checkbox_text'>
<%= todo.text %>
</div>用于静态表单的jQuery代码:
$('.checkbox_text').click(function (event) {
$('#todo_status').submit();
});此外,如何在没有:id的情况下对t.text_field进行解析?
发布于 2017-01-31 11:31:33
如果checkox在表单中,其间没有任何元素,则只需检查复选框的父项:
$('.checkbox_text').click(function (event) {
$(this).parent().submit(); // $(this).parent() would return the form itself
});当您将id放入元素时,这意味着它是唯一的。您不可能在dom中有很多元素具有相同的id --希望选择所有这些元素。
发布于 2017-01-31 11:38:37
您可以尝试$(selector).closest()从文本中获取表单。
$(document).on('click','.checkbox_text',function (event){
$(this).closest('#formId').submit() // You can use class name also like('.formClass')
})https://stackoverflow.com/questions/41956402
复制相似问题