简体   繁体   中英

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

I have a form with a javascript submit function. 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

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. 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. Don't use links / JS to submit forms.

Just use submit buttons. You can even send your identifying values without involving 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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