Rails 尝试在下拉列表更改时提交表单

发布于 2024-11-28 12:40:33 字数 1356 浏览 0 评论 0原文

我的 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="&#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> 

提前感谢您的帮助。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

老子叫无熙 2024-12-05 12:40:33

用这个替换你的 onchange ,

onchange: "this.form.submit();"

Replace your onchange with this,

onchange: "this.form.submit();"
带上头具痛哭 2024-12-05 12:40:33

如果表单为 remote: true 且正在使用 rails-ujs,则 this.form.submit() 将不起作用。在这种情况下,将进行常规提交而不是 XHR。

相反,您应该:

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

您可以阅读此处了解更多详细信息。

this.form.submit() will not work if form is remote: true and rails-ujs is in use. In that case, a regular submit will occur instead of XHR.

Instead you should:

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

You can read here for more details.

还如梦归 2024-12-05 12:40:33

这就是我能够做的让它发挥作用。我使用 :name => 将表单命名为 switch_car “switch_car”并使用了以下 JavaScript。

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

我仍在寻找更好的答案,因此如果我找到什么我会更新。由于某种原因,这不使用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.

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

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.

撩发小公举 2024-12-05 12:40:33

这是一个老问题,但以上答案都不适合我。它们都会产生一个 text/html 请求。

以下作品,例如(隐藏类设置CSS显示:无):

<%= form.radio_button :tier_id, tier.id, onchange: "$('#submit-button-id').click();" %>

与:

<%= form.submit "Save changes", id: 'submit-button-id', class: 'hide' %>

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):

<%= form.radio_button :tier_id, tier.id, onchange: "$('#submit-button-id').click();" %>

together with:

<%= form.submit "Save changes", id: 'submit-button-id', class: 'hide' %>
无所谓啦 2024-12-05 12:40:33

根据您使用的 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.

相守太难 2024-12-05 12:40:33

使用 jQuery 的更通用的解决方案(我不需要知道表单的名称)是:

onchange: "$(this).parent('form').submit();"

A more general solution using jQuery (I don't need to know the name of the form) is:

onchange: "$(this).parent('form').submit();"
野心澎湃 2024-12-05 12:40:33

这似乎已经存在了一段时间,但是,无论如何我都会发布我的发现,因为我还没有在任何地方找到这个解释。

我发现了这篇文章 很好地描述了通过 submit() 方法(带或不带 jQuery 或 Handler)触发 ajax 请求时出现的问题。然后作者建议形成您自己的 AJAX 请求。这不是必需的,也不应该这样做来利用 rails.js(jquery-jus gem)。

由于rails.js 绑定并侦听命名空间为submit.rails 的事件,因此手动触发submit() 会出现问题。要手动触发提交,请

   onchange: 'javascript: $( this ).trigger("submit.rails")'

在元素或表单上使用。

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 namespaced submit.rails. To manually trigger a submission use

   onchange: 'javascript: $( this ).trigger("submit.rails")'

on the element or the form.

萌酱 2024-12-05 12:40:33

对于 select_tag,只需添加:

{:onchange => "myHandler();" }

您的处理程序将在哪里:

this.form.submit();

另外,如果 onchange 不起作用,您可能需要尝试使用大写 C 的 onChage。

最后,请确保不要将 select_tag 与表单选择混淆。

请参阅我对类似问题的回答,仅涉及表单选择

向表单选择添加 Onchange 事件

For a select_tag, just add:

{:onchange => "myHandler();" }

where your handler will be:

this.form.submit();

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

小苏打饼 2024-12-05 12:40:33

时间在2021年,用Rails 6

进行隐藏提交然后使用js click()函数:

<%= form_with(modle: @user, local: false) do |f| %>
    <%= f.select :menu1, ["option1","option2"], {}, onchange: "javascript:this.form.commit.click();" %>
    <%= f.submit 'save', class: "hidden" %>
<% end>

参考:https: //stackoverflow.com/a/8690633/15461540

Time at 2021, with Rails 6

make a hidden submit then use js click() function:

<%= form_with(modle: @user, local: false) do |f| %>
    <%= f.select :menu1, ["option1","option2"], {}, onchange: "javascript:this.form.commit.click();" %>
    <%= f.submit 'save', class: "hidden" %>
<% end>

reference: https://stackoverflow.com/a/8690633/15461540

肥爪爪 2024-12-05 12:40:33

如果你想学习 Rails 方式的 ajax 提交字段更改,这里是代码。

<%= select_tag(:id, options_from_collection_for_select(active_cars, "id", "name"), 
data: { remote: true, url: your_ajax_route_path }%><%= submit_tag "Switch Car" %>

数据:{远程:true,url:your_ajax_route_path}

会自动将您的输入提交到 your_ajax_route_path。 使用 input_html

input_html: { data: { remote: true, url: your_ajax_route_path } }

如果您使用的是表单生成器,那么您应该像这样 。我希望它对你有用

If you want to learn rails way ajax submit on fields change here is the code.

<%= select_tag(:id, options_from_collection_for_select(active_cars, "id", "name"), 
data: { remote: true, url: your_ajax_route_path }%><%= submit_tag "Switch Car" %>

data: { remote: true, url: your_ajax_route_path }

will automatically submit your input to your_ajax_route_path. If you are using a form builder then you should use with input_html

input_html: { data: { remote: true, url: your_ajax_route_path } }

like this. I hope it'll be useful for you

笑,眼淚并存 2024-12-05 12:40:33

这些解决方案都不适合我,所以我使用了下面给出的解决方案。

$("#q_dimension_id_eq").on("change", function (e) {
        $(this).closest("form").submit();
    });

None of the solutions was working for me so I used the given below solution.

$("#q_dimension_id_eq").on("change", function (e) {
        $(this).closest("form").submit();
    });

小傻瓜 2024-12-05 12:40:33

我正在使用带有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文