有没有办法在一个表单中添加两个提交按钮,并且其中一个请求应该是远程的?我的意思是不使用额外的JS。
我的问题与Rails:在一个表单中使用多个提交按钮相关,但不同之处在于远程。
有没有办法在一个表单中添加两个提交按钮,并且其中一个请求应该是远程的?我的意思是不使用额外的JS。
我的问题与Rails:在一个表单中使用多个提交按钮相关,但不同之处在于远程。
我遇到了同样的问题,并使用这个答案中的解决方案,在单击相应按钮时通过 JQuery 使表单变为远程或非远程。为此,请将class:'local-button'
和class:'remote-button'
添加到您的提交按钮,id:'my-form'
添加到您的表单,以及以下代码(CoffeeScript):
$(".remote-button").click ->
$("#my-form").attr("data-remote", true)
$(".local-button").click ->
$("#my-form").removeAttr("data-remote")
$("#my-form").removeData("remote");
最简单的方法可能是在同一页上有两个不同的表单。请参见下面。
HelloController
class HelloController < ApplicationController
def say_hello
puts params
respond_to do |format|
format.html
format.js
end
end
end
routes.rb
TwoForms::Application.routes.draw do
get "hello/get_ready"
post "hello/say_hello"
root to: "hello#get_ready"
end
views/hello/get_ready.html.erb
<h2>Regular hello</h2>
<%= button_to "Regular Hello", hello_say_hello_path, name: "regular" %>
<h2>Ajaxy hello</h2>
<%= button_to "Ajaxy Hello", hello_say_hello_path, name: "ajaxy", remote: true %>
views/hello/say_hello.html.erb
Success (regular)!
<%= params %>
views/hello/say_hello.js.erb
alert("Success (Ajaxy)");
$("#remote_btn_submit").on("click", function(){ //click it's not the best bind perhaps input?
$("#my_form").data("remote", "true");
//or
//$("#my_form").attr("data-remote", "true");
});
你还需要在远程提交中使用preventDefault