setting target _blank for just one form submit action

前端 未结 3 1317
刺人心
刺人心 2021-01-11 18:22

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

3条回答
  •  伪装坚强ぢ
    2021-01-11 19:22

    you can intercept click to the button, read it's data and change form before submit:

    So, HTML:

    JS

    $('button.Submit').click( function() {
        var t=$(this);
        var form=t.parents('form');
        form.attr('target',t.data('target'));
        form.submit();
        return false;
    });
    

    this way you can control the target option in your html markup.

    http://jsfiddle.net/oceog/gArdk/

    in case if you not clear target of the form, you will get the following scenario:

    • user click on popup button,
    • submitted the form,
    • closed window,
    • click on non-popup

    and that will also popup him form target.

    so in my snipplet I clear target in case of data-target=''

    if you want mark as popup only one element, you will need to clone your form: http://jsfiddle.net/oceog/gArdk/2/

    JS:

    $('button.Submit.popup').click( function() {
        var t=$(this);
        var form=t.parents('form').clone(true).attr('target','_blank');
        
        form.submit();
        return false;
    });
    

    HTML:

提交回复
热议问题