带有两个提交按钮和两个“目标"的 HTML 表单属性
我有一个 HTML .
I have one HTML <form>.
表单只有一个 action=""
属性.
The form has only one action=""
attribute.
但是我希望有两个不同的 target=""
属性,这取决于您单击哪个按钮来提交表单.这可能是一些花哨的 JavaScript 代码,但我不知道从哪里开始.
However I wish to have two different target=""
attributes, depending on which button you click to submit the form. This is probably some fancy JavaScript code, but I haven't an idea where to begin.
如何创建两个按钮,每个按钮提交相同的表单,但每个按钮为表单提供不同的目标?
How could I create two buttons, each submitting the same form, but each button gives the form a different target?
用这样的心态来解决这个问题更合适,即表单将有一个绑定到一个提交按钮的默认操作,然后另一个绑定到一个提交按钮的替代操作普通按钮.这里的区别在于,提交下的任何一个都会是用户按 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.