如何使表单在提交 url 操作时不打开新窗口


How to Make a form not open a new window when it submits a url action?

我有一个提交网址操作的表单。但是,我不希望它在提交时打开一个带有 url 的新窗口。我应该如何避免这种情况?

<form name="leds" id="ledSend" method="get" target="_blank" action="https://agent.electricimp.com/Fk43xPMkSrWF">
Lamp Control: <input type="radio" name="led" value="0" checked>Off
            <input type="radio" name="led" value="1">On<br>
How long should the Lights stay on? <input type="text" name="timer" value="10">seconds<br>
Your name? For Our Records <input id="name" type="text" name="user" placeholder="Your name here">       <br>
<input type="submit" value="Update!" onclick="alert(theInput.value)"/>
<script>

var theInput = document.getElementById("name");

我不希望此表单将窗口更改为

https://agent.electricinp.com

提交时。我将如何避免这种情况,以便用户停留在他们提交表单表单的原始页面上。

摆脱

target="_blank"

<form name="leds" id="ledSend" method="get" action="">

最近我得到了这个场景,并研究了很多以找到黑客/解决方案。

解决方案 - 如果您可以输入窗口名称并每次都引用相同,那么浏览器将确保打开新选项卡(如果尚未打开),否则它只会刷新窗口。

示例片段:此处演示 JSfiddle

<form id="myForm" action="<URL>" method="POST" target="_blank" onsubmit="target_popup(this)">
    First name: <input type="text" name="fname"/><br/>
    Last name: <input type="text" name="lname"/><br/>
    <button type="submit" id="btnTest"> Submit</button>
</form>

<script>
var target_popup = function(form) {
    window.open('',//URL should be blank so that it will take form attributes.
                'UniqueWindowName', //window name
                'width=400,height=400,resizeable,scrollbars');
    form.target = 'UniqueWindowName';
}
</script>

请参阅我的博客了解更多详情