使用外部提交按钮提交 Rails Remote_form

发布于 2024-08-23 07:27:18 字数 1055 浏览 3 评论 0原文

我正在尝试使用表单标签之外的提交触发器来提交 Rails 表单。所以我有我的表单:

<% remote_form_for Package.new, 
:url => { :action => :rate_shop, :ids_sales_order_id => params[:ids_sales_order_id],
:id => @shipment.id }, :loading => '$("#loading").overlay({api: true}).load(); ' + visual_effect(:appear, 'priceShopInProgress'), 
:complete => visual_effect(:fade, 'priceShopInProgress', :duration => 2), 
:update => 'itemSelection' do |f| %>

我有我的 jQuery:

$('#priceSelectSubmit').click(function() {
  $('#new_item').submit();
});

和我的触发器:

<%= submit_tag :Submit, :value => 'Select item', :id => 'priceSelectSubmit', :name => 'priceSelectSubmit' %>

事情就是这样。提交按钮正确提交表单(并加载包含所有正确数据的新页面),但不会像按钮位于表单标签内那样触发表单(特别是 :loading 内容非常重要)。我需要 AJAX 响应和要加载的覆盖层等。

我的限制:

  • 必须使用 jQuery 和 Rails
  • 提交按钮不能在表单内
  • 我不是这方面的主要程序员(他不在 atm 上)所以我不知道如果这只是我的 jquery 的一个简单调整以使表单正确触发,或者如果它需要将“action”内容从表单标记移出并放入 jquery 函数中。有什么想法吗?非常感谢!

I'm trying to submit a rails form using a submit trigger that is outside of the form tags. So I have my form:

<% remote_form_for Package.new, 
:url => { :action => :rate_shop, :ids_sales_order_id => params[:ids_sales_order_id],
:id => @shipment.id }, :loading => '$("#loading").overlay({api: true}).load(); ' + visual_effect(:appear, 'priceShopInProgress'), 
:complete => visual_effect(:fade, 'priceShopInProgress', :duration => 2), 
:update => 'itemSelection' do |f| %>

And I have my jQuery:

$('#priceSelectSubmit').click(function() {
  $('#new_item').submit();
});

And my trigger:

<%= submit_tag :Submit, :value => 'Select item', :id => 'priceSelectSubmit', :name => 'priceSelectSubmit' %>

Here's the deal. The submit button submits the form correctly (and loads a new page with all the right data) but does not fire off the form like it would if the button were within the form tags (specifically the :loading stuff is pretty important). I need that AJAX response and the overlay to load, etc.

My contraints:

  • Must use jQuery with Rails
  • Submit button can not be within form
  • I am not the main programmer on this (he is out of action atm) so I don't know if it's just a simple tweak of my jquery to get the form to fire correctly, or if it requires moving the "action" stuff out of the form tag and into a jquery function. Any thoughts? Very appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

尴尬癌患者 2024-08-30 07:27:18

好吧,我通过反复谷歌搜索找到了答案。感谢塔德曼让我走上了正确的道路!

$('form').trigger('onsubmit')

这就是 jQuery 让您提交表单的方式,就像用户单击“提交”一样。

Ok so I figured it out from repeated googling. And thanks tadman for sending me down the right path!

$('form').trigger('onsubmit')

That's how jQuery lets you submit a form as if a user clicked submit.

小女人ら 2024-08-30 07:27:18

您可以尝试使用 onsubmit() 调用:

$('#priceSelectSubmit').click(function() {
  $('#new_item')[0].onsubmit();
});

当您通过用户操作“提交”表单时,会调用 onsubmit 函数,但当您直接调用表单上的提交方法时,通常会绕过 onsubmit 函数。

如果查看实际生成的 HTML,您可以看到 onsubmit="..." 块内进行的 Ajax 调用。

编辑:修改为取消引用 jQuery 代理对象。

You could try using the onsubmit() call instead:

$('#priceSelectSubmit').click(function() {
  $('#new_item')[0].onsubmit();
});

The onsubmit function is called when you "submit" the form via a user action, but is typically bypassed when you call the submit method on the form directly.

If you view the actual HTML generated, you can see the Ajax call made inside the onsubmit="..." block.

Edit: Amended to de-reference the jQuery proxy object.

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