简体   繁体   中英

On form submit, open a new page in a new tab and redirect the original page

I have three pages. Page-1 has a form that when submitted, should open up page-2 in a new tab and page-1 should redirect to page-3.

Right now, clicking submit opens page-2 in a new tab, but it doesn't redirect page-1 unless I already happen to have page-2 open. If page-2 is already open and I click submit, then page-1 redirects to page-3.

How can I get both to occur?

In view template:

%button.step1-button.submit{:onClick => "redirect();"} Get Started

In controller:

@form = FormSubmit.create(:name => name, :form => "/")
        respond_to do |format|
          format.html {redirect_to :page_2}
          format.js { render :action => "redirect()"}

In redirect.js:

function pop_under2() {
  $('.step1-button').bind("click", function () {
    window.location.href = "http://localhost:3000/exit";
  });
};

You could simply make the button a link with right href and target set, which has an additional event listener, which submits the form. The controller could redirect afterwards.

Template

%a.step1-button.submit.js-submit-form{href: "your_page2_url", target: "_blank"} Get Started

Controller

@form = FormSubmit.create(:name => name, :form => "/")
redirect_to :page_3

JS

$(function(){
  $('.js-submit-form').on('click', function() {
    $(this).parent('form').submit();
  });
}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM