简体   繁体   English

在新标签页中打开链接时,表单提交不会发生

[英]Form submit doesn't happen while opening the link in a new tab

I have a form with a javascript submit function. 我有一个带有javascript提交功能的表单。 I have 3 links (anchor tags) in the form & would like to set different values to the hidden parameter on submit based on the link clicked 我在表单中有3个链接(锚标签),并希望根据所单击的链接为提交的隐藏参数设置不同的值

Form submit works fine generally, but if I try to open the link in a new tab, form is not submitted. 表单提交通常可以正常工作,但是如果我尝试在新标签页中打开链接,则不会提交表单。 The alert within the javascript function is not printed. javascript函数中的警报未打印。 Does anyone have any idea to fix this? 有谁知道要解决此问题?

Below is the Sample Code : 下面是示例代码:

<html>

    <head>
        <script type="text/javascript">
            function submitForm(actionParam) {
            alert("in submitForm");
            document.getElementById("action").value = actionParam;
            document.forms['myForm'].action = action;
            document.forms['myForm'].submit();
        }
        </script>
    </head>

    <body>

        <form name="myForm" id="myForm" method="post" action="/businesspanel">
            <input type="hidden" id="action" name="action" value="" />

            <a href="#" onclick="submitForm('action1');">Action 1</a></span>
            <a href="#" onclick="submitForm('action2');">Action 2</a></span>
            <a href="#" onclick="submitForm('action3');">Action 3</a></span>

        </form>

    </body>
</html>

Opening a link in a new window bypasses the JavaScript. 在新窗口中打开链接会绕过JavaScript。 Don't use links / JS to submit forms. 不要使用链接/ JS提交表单。

Just use submit buttons. 只需使用提交按钮。 You can even send your identifying values without involving JS. 您甚至可以在不涉及JS的情况下发送您的标识值。

<button name="action" value="action1">Action 1</button>
<button name="action" value="action2">Action 2</button>
<button name="action" value="action3">Action 3</button>
document.forms['myForm'].action = action;

此处未定义“操作”。如果您想在新标签页中打开链接,请执行以下操作:

document.forms['myForm'].target= '_blank';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM