Rails 尝试在下拉列表更改时提交表单
我的 Ajax 工作正常,内置 Rails javascript,带有提交按钮。但是,我希望它在更改下拉框的值并消除按钮时提交。在我的研究中,我找到了看起来正确的解决方案,但我没有收到对服务器的请求。这是我的下拉表单代码,请注意,它仍然具有在我添加之前起作用的提交按钮: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:
<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="✓" />
<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>
提前感谢您的帮助。
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 %>
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="✓" />
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
用这个替换你的 onchange ,
Replace your onchange with this,
如果表单为
remote: true
且正在使用rails-ujs
,则this.form.submit()
将不起作用。在这种情况下,将进行常规提交而不是 XHR。相反,您应该:
您可以阅读此处了解更多详细信息。
this.form.submit()
will not work if form isremote: true
andrails-ujs
is in use. In that case, a regular submit will occur instead of XHR.Instead you should:
You can read here for more details.
这就是我能够做的让它发挥作用。我使用 :name => 将表单命名为 switch_car “switch_car”并使用了以下 JavaScript。
我仍在寻找更好的答案,因此如果我找到什么我会更新。由于某种原因,这不使用submit .js。与使用 AJAX 仅更新更改的页面元素的提交按钮不同,它将其作为 HTML 进行处理。但这是迄今为止我能找到的最好的。
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.
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.
这是一个老问题,但以上答案都不适合我。它们都会产生一个 text/html 请求。
以下作品,例如(隐藏类设置CSS显示:无):
与:
This is an old question, but none of the above answers work for me. They all result in a text/html request.
The following works, e.g. (the hide class sets css display: none):
together with:
根据您使用的 js 库:
原型:
:onchange => ("$('switch_car').submit()")
Jquery:
:onchange => ("$('#switch_car').submit()")
如果您使用默认值并且您的 Rails 版本低于 3.1,则原型是默认库。
Depending on the js library you are using:
Prototype:
:onchange => ("$('switch_car').submit()")
Jquery:
:onchange => ("$('#switch_car').submit()")
If you are using the defaults and your rails version is below 3.1, then prototype is the default library.
使用 jQuery 的更通用的解决方案(我不需要知道表单的名称)是:
A more general solution using jQuery (I don't need to know the name of the form) is:
这似乎已经存在了一段时间,但是,无论如何我都会发布我的发现,因为我还没有在任何地方找到这个解释。
我发现了这篇文章 很好地描述了通过
submit()
方法(带或不带 jQuery 或 Handler)触发 ajax 请求时出现的问题。然后作者建议形成您自己的 AJAX 请求。这不是必需的,也不应该这样做来利用 rails.js(jquery-jus gem)。由于rails.js 绑定并侦听命名空间为
submit.rails
的事件,因此手动触发submit()
会出现问题。要手动触发提交,请在元素或表单上使用。
This seems to have been around for a while but, I'll post my findings anyway since I haven't found this explanation anywhere yet.
I came across this article which describes quite well what's the problem when triggering a ajax request via the
submit()
method (with or without jQuery or Handler). The author then recommends to form your own AJAX request. That is not required and shouldn't be done to make use of the logic within rails.js (jquery-jus gem).Problems with triggering
submit()
manually occur since rails.js binds and listens to an event that is namespacedsubmit.rails
. To manually trigger a submission useon the element or the form.
对于 select_tag,只需添加:
您的处理程序将在哪里:
另外,如果 onchange 不起作用,您可能需要尝试使用大写 C 的 onChage。
最后,请确保不要将 select_tag 与表单选择混淆。
请参阅我对类似问题的回答,仅涉及表单选择
向表单选择添加 Onchange 事件
For a select_tag, just add:
where your handler will be:
Also, if onchange doesn't work you might want to try onChage with a capital C.
Finally, make sure NOT TO CONFUSE a select_tag with a form select.
See my answer to a similar question, only regarding a form select
Adding An Onchange Event To A Form Select
时间在2021年,用Rails 6
进行隐藏提交然后使用js
click()
函数:参考:https: //stackoverflow.com/a/8690633/15461540
Time at 2021, with Rails 6
make a hidden submit then use js
click()
function:reference: https://stackoverflow.com/a/8690633/15461540
如果你想学习 Rails 方式的 ajax 提交字段更改,这里是代码。
会自动将您的输入提交到 your_ajax_route_path。 使用 input_html
如果您使用的是表单生成器,那么您应该像这样 。我希望它对你有用
If you want to learn rails way ajax submit on fields change here is the code.
will automatically submit your input to your_ajax_route_path. If you are using a form builder then you should use with input_html
like this. I hope it'll be useful for you
这些解决方案都不适合我,所以我使用了下面给出的解决方案。
None of the solutions was working for me so I used the given below solution.
我正在使用带有
jquery_ujs
的 Rails 4.2.1,它对我有用:onchange: 'jQuery(this).parent().trigger("submit")'
OBS .:它假定该元素是表单的直接子元素。您必须根据您的 DOM 树调整
parent()
。I'm using rails 4.2.1 with
jquery_ujs
and it works for me:onchange: 'jQuery(this).parent().trigger("submit")'
OBS.: It assumes the element is immediately child from form. You must adjust
parent()
according your DOM tree.