【问题标题】:Submit dynamics form提交动态表格
【发布时间】:2017-01-31 19:38:37
【问题描述】:

我有许多带有 check_box 元素的动态创建表单 (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();
});

另外,我如何在没有t.text_field 的情况下将参数设置为:id

【问题讨论】:

  • 你试过$(codument).on('click','.checkbox_text',function (event)
  • @Carsten Løvbo Andersen 没用 :(
  • 您是否收到任何console 错误?按 F12
  • @Carsten Løvbo Andersen 没有错误。

标签: jquery ruby-on-rails


【解决方案1】:

如果 checkox 在表单内且两者之间没有元素,则只需检查复选框的父项:

$('.checkbox_text').click(function (event) {
  $(this).parent().submit(); // $(this).parent() would return the form itself
});

当你给一个元素加上一个 id 时,这意味着它是唯一的。您不能在 dom 中有很多具有相同 id 的元素并希望选择所有元素。

【讨论】:

  • 谢谢。它的工作很棒。也许你也知道这个:Also, how can I put param my :id without t.text_field ?
【解决方案2】:

您可以尝试 $(selector).closest() 从文本中获取表单。

$(document).on('click','.checkbox_text',function (event){
    $(this).closest('#formId').submit() // You can use class name also like('.formClass')
})

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2021-05-21
    • 2019-01-09
    相关资源
    最近更新 更多