首页 > 解决方案 > 基于点击事件远程提交简单表单

问题描述

我正在尝试创建一个待办事项应用程序,其中复选框是使用简单表单创建的,并且需要在单击复选框时远程提交。我正在使用 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-railsajaxsimple-form

解决方案


因为rails-ujs它是通过正常的 HTML 请求提交的。相关问题答案

试试这个方法:

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

推荐阅读