且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有两个提交按钮和两个“目标"的 HTML 表单属性

更新时间:2021-12-27 22:27:47

用这样的心态来解决这个问题更合适,即表单将有一个绑定到一个提交按钮的默认操作,然后另一个绑定到一个提交按钮的替代操作普通按钮.这里的区别在于,提交下的任何一个都会是用户按 Enter 提交表单时使用的那个,而另一个只会在用户明确点击按钮时被触发.

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:

<form id='myform' action='jquery.php' method='GET'>
    <input type='submit' id='btn1' value='Normal Submit'>
    <input type='button' id='btn2' value='New Window'>
</form>

使用这个 javascript:

With this javascript:

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

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

将代码绑定到提交按钮的点击事件的方法不适用于 IE.

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