javascript 在 <a href="form.submit();"> 中提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15679697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:40:55  来源:igfitidea点击:

Submit form in <a href="form.submit();">

javascriptjsp

提问by AL??I

Sorry for asking a weird question... I want to submit a form out of . But I want to allow users to open the link in a new tab (e.g Ctrl+Click). Here is what I have already tried.

抱歉问了一个奇怪的问题...我想提交一个表格。但我希望允许用户在新选项卡中打开链接(例如 Ctrl+单击)。这是我已经尝试过的。

1 . <a href="#" onclick="document.form1.submit();">Submit Form</a>

This opens the new link in the same tab and the old link in the new tab.

这将在同一选项卡中打开新链接,并在新选项卡中打开旧链接。

2. <a href="javascript: document.form1.submit();">Submit Form</a>

This does not let the link to be opened in a new tab.

这不会让链接在新选项卡中打开。

How can I make it right?

我怎样才能做到正确?

Thanks.

谢谢。

回答by archil

use the targetattribute on form

在表单上使用目标属性

<form action="demo_form.asp" method="get" target="_blank">
  <input type="submit" value="Submit">
</form>

回答by JoDev

To open the submission into a new tab, use :

要在新选项卡中打开提交,请使用:

<form name="myform" id='myform' [...] target='_blank'>
[...]
</form>

[TO MAKE NEW TAB OPTIONAL]
If you want, you can add a checkbox, with this code attached :

[使新选项卡可选]
如果需要,您可以添加一个复选框,并附上以下代码:

<a href="#" onclick="document.form1.submit();">Submit Form</a><br />
<input type='checkbox' id='newTabCheck' /> Open in new Tab

And in jQuery

在 jQuery 中

$('#form1').submit(function() {
   if($('#newTabCheck').is(':checked'))
    $('#form1').attr('target', '_blank');
    return true;
});

Or

或者

$('#newTabCheck').change(function() {
   if($(this).is(':checked'))
      $(this).attr('target', '_blank');
   else
      $(this).removeAttr('target');
});

回答by Vishnu R

Just put target="_blank"in <form></form>Fields.

只要把target="_blank"<form></form>各个领域。

ie <form action"your_url" method="post" target="_blank"></form>

IE <form action"your_url" method="post" target="_blank"></form>