在 rails 中使用 link

编程入门 行业动态 更新时间:2024-10-28 08:23:57
本文介绍了在 rails 中使用 link_to 提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 link_to 提交表单,如下所示:

I'm trying to submit a form using link_to as follows:

<%= form_for(@post, :url=> '/post/action', :method=> 'post', :html => {:id=>'form_id'} ) do |f| %> .... <%= link_to 'submit', "/post/action", :onclick=>"document.getElementById('form_id').submit()" %> ....

但它不是发布表单,它只是将我的表单重定向到指定的 url.有人知道怎么做吗?

but it is not posting the form, it is simply redirecting my form to the specified url. Does anyone know how to do this?

推荐答案

您可以使用:

<%= link_to 'submit', "#", :onclick => "$('#form_id').submit()" %>

如果您使用的是 JQuery 和更高版本的 rails.

if you are using JQuery and a later version of rails.

上面可以工作,但如果你有一个很长的页面,它会因为#"而导航到页面顶部,所以如果你想避免这种情况,你可以这样做:

Above will work but if you have a really long page it will navigate to top of the page because of "#" so if you want to avoid that you can do:

<%= link_to 'submit', "", :onclick => "$('#form_id').submit()" %>

更多推荐

在 rails 中使用 link

本文发布于:2023-10-09 17:52:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rails   link

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!