只为一个表单提交操作设置目标_blank [英] setting target _blank for just one form submit action

查看:174
本文介绍了只为一个表单提交操作设置目标_blank的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两个提交按钮将表单提交给我的Rails应用程序。他们都采用相同的控制器操作,并使用相同的表单,但是在一个提交操作中,我希望使表单 target = _blank ,以便表单提交结果在一个新的窗口。



有没有简单的方法来做到这一点?





 <%= simple_form_for @thingy,:target => '_blank'do | f | %GT; 

我使用jQuery来设置目标窗体 onclick ,但没有运气。

解决方案

在其中一个按钮上添加一个类, code>弹出。然后添加一些像这样的jQuery:

$ p $ $('。popup')。click(function(e){
e.preventDefault(); //防止默认提交动作
$(this).closest('form')。attr('target','_blank')。submit();
}) ;

演示: http://jsfiddle.net/HxrzD/



请注意,还有其他方式可以提交您可能需要考虑的表单,f .ex 输入键。如果用户返回到父窗口并使用另一个按钮再次提交,您可能还需要重置表单的目标属性。在这种情况下,您可以在最后清除目标:

  $(this).closest('form')。 attr('target','_blank')。submit()。removeAttr('target'); 

或保存之前的状态:

  var $ form = $(this).closest('form'),
target = $ form.attr('target');
$ form.attr('target','_blank')。submit()。attr('target',target);

http://jsfiddle.net/HxrzD/2/


I'm submitting a form to my rails app, with two submit buttons. They both go to the same controller action, and use the same form, but on one submit action i want to make the form target=_blank so that the form submission results open in a new window.

Is there an easy way to do this?

Doing this works for both actions:

<%= simple_form_for @thingy, :target => '_blank' do |f| %>

I've mucked around with using jQuery to set the target on the form onclick, but with no luck.

解决方案

On one of the buttons, add a class say popup. Then add some jQuery like this:

$('.popup').click(function(e) {
    e.preventDefault(); //prevents the default submit action
    $(this).closest('form').attr('target', '_blank').submit();
});

Demo: http://jsfiddle.net/HxrzD/

Note that there are other ways to submit a form that you might need to consider, f.ex the enter key. You also might need to reset the form’s target attribute in case the user goes back to the parent window and submits again using the other button. In that case you can either just clear the target at the end:

$(this).closest('form').attr('target', '_blank').submit().removeAttr('target');

Or save the previous state:

var $form = $(this).closest('form'),
    target = $form.attr('target');
$form.attr('target', '_blank').submit().attr('target', target);

http://jsfiddle.net/HxrzD/2/

这篇关于只为一个表单提交操作设置目标_blank的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆