Rails尝试提交下拉式变化的表单 [英] Rails Trying to submit a form onchange of dropdown

查看:109
本文介绍了Rails尝试提交下拉式变化的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Ajax工作,内置Rails javascript,提交按钮。但是,我希望在更改下拉框的值并删除按钮时提交。在我的研究中,我发现什么看起来像正确的解决方案,但我没有得到服务器的请求。这里是我的下拉表单代码,请注意,它仍然有提交按钮,我添加之前工作:onchange:

I have my Ajax working, builtin Rails javascript, with the submit button. However, I would like it to submit when I change the value of the dropdown box and eliminate the button. In my research I found what looks like the correct solution but I get no request to the server. Here is my dropdown form code, note it still has the submit button that worked before I added :onchange:

<% form_tag('switch_car', :method => :put, :remote => true) do %>
  <div class="field">
    <label>Car Name:</label>
    <%= select_tag(:id, options_from_collection_for_select(active_cars, "id", "name"), 
       :onchange => ("$('switch_car').submit()"))%><%= submit_tag "Switch Car" %>
  </div>    
<% end %>

以下是生成的HTML:

Here is the HTML generated:

<form accept-charset="UTF-8" action="switch_car" data-remote="true" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="&#x2713;" />
    <input name="_method" type="hidden" value="put" />
    <input name="authenticity_token" type="hidden" value="PEbdqAoiik37lcoP4+v+dakpYxdpMkSm7Ub8eZpdF9I=" />
  </div>
  <div class="field"> 
    <label>Car Name:</label> 
    <select id="id" name="id" onchange="$('switch_car').submit()">
      <option value="9">Truck</option>
      <option value="10">Car</option>
    </select>
    <input name="commit" type="submit" value="Switch Car" /> 
</div> 

在此先感谢您的帮助。

Thanks in advance for any help.

推荐答案

这是我能够做到的。我通过使用name =>switch_car来命名表格switch_car,并使用以下javascript。

This is what I was able to do to get it to work. I named the form switch_car by using :name => "switch_car" and used the following javascript.

:onchange => ("javascript: document.switch_car.submit();")

我仍在寻找更好的回答,所以我会更新,如果我找到了什么。由于某种原因,这不使用submit .js。它将它处理为HTML,而不像使用AJAX更新仅更新页面元素的提交按钮。但这是迄今为止我能找到的最好的。

I am still looking for a better answer so I will updated if I find something. This doesn't use submit .js for some reason. It processes it as HTML unlike the submit button which uses AJAX to update only the changing page elements. But this is the best I have been able to find so far.

这篇关于Rails尝试提交下拉式变化的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆