HTML form with two submit buttons and two “target” attributes

前端 未结 15 2294
暗喜
暗喜 2020-11-29 20:02

I have one HTML

.

The form has only one action=\"\" attribute.

However I wish to have two different target=\"\" attribu

15条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 20:29

    It is more appropriate to approach this problem with the mentality that a form will have a default action tied to one submit button, and then an alternative action bound to a plain button. The difference here is that whichever one goes under the submit will be the one used when a user submits the form by pressing enter, while the other one will only be fired when a user explicitly clicks on the button.

    Anyhow, with that in mind, this should do it:

    
        
        
    
    

    With this javascript:

    var form = document.getElementById('myform');
    form.onsubmit = function() {
        form.target = '_self';
    };
    
    document.getElementById('btn2').onclick = function() {
        form.target = '_blank';
        form.submit();
    }
    

    Approaches that bind code to the submit button's click event will not work on IE.

提交回复
热议问题