【问题标题】:Submit simple form remotely on the basis of a click event基于点击事件远程提交简单表单
【发布时间】:2020-02-12 00:08:06
【问题描述】:

我正在尝试创建一个待办事项应用程序,其中复选框是使用简单表单创建的,并且需要在单击复选框时远程提交。我正在使用 onchange() 函数,但表单没有被远程提交。它通过整页刷新提交。

当包含提交按钮时,它会远程提交表单并且一切正常。但我无法在没有提交按钮的情况下让它工作,而只能使用复选框。

_goal.html.erb

<li id='<%= "Goal_li_#{goal.id}" %>'>
  <div class="collapsible-header"  tabindex="0">
    <label>
      <%= simple_form_for(goal, url:goal_path(goal),remote: true,authenticity_token: true) do |f| %>
        <%= f.check_box :is_complete,onchange: 'this.form.submit();', disabled:goal_disabled_class(goal), class: "check_box_check" %>
        <span class=<%= "line-through-goal" if goal.is_complete==1 %>>
          <%= goal.title %>-<%= goal.id %>-<%= goal.status %>
        </span>

        <%= "<span class='new badge blue right'> #{goal.sub_goals.where(is_complete: 0).count} </span>".html_safe if goal.sub_goals.where(is_complete: 0).count > 0  %>
      <% end %>
    </label>
  </div>
</li>

这会导致页面刷新,而不是远程提交表单。我发现的所有示例都在使用提交按钮。如何根据复选框点击触发远程执行?

【问题讨论】:

    标签: ruby-on-rails ajax simple-form


    【解决方案1】:

    由于rails-ujs,它是通过普通的HTML请求提交的。相关issueanswer

    试试这个方法:

    onchange: 'Rails.fire(this.form, "submit")'
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2019-01-03
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多