2013-03-28 129 views
2

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.在<a href="form.submit();">

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.

回答

2

use the target属性提交表单形式

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

这将始终打开一个新的标签,这是我不希望的链接。 – 2013-03-28 10:57:22

2

要打开提交到一个新的标签,使用:

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

[作出新的TAB可选]
如果你愿意,你可以添加复选框,使用此代码附:

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

而在jQuery的

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

或者

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

我提到了(Ctrl + Click),这意味着这应该是可选的。并非总是在新选项卡中打开链接。 – 2013-03-28 10:59:30

1

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

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

+0

我不想一直打开新选项卡中的链接。我想给用户做一个选项。 :) – 2013-03-28 10:55:38