sTr*_*gin 0 html javascript forms
所以我知道,当您将 URL 放入 HTML 表单的操作中时,它会将用户发送到该 URL。我还添加了目标属性target="_blank"以在新窗口中打开新 URL。但我想关闭该窗口并留在表单提交表单的原始站点上。我试图通过命名目标窗口并关闭它来做到这一点,但没有成功。我想知道是否有一种简单的方法可以做到这一点。
<form name="leds" id="ledSend" method="get" target="newWindow" action="https://agent.electricimp.com/NkzPvVKeHshT">
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 +', You are about to change the Light! Keep in mind that there will be about a 2 second delay on the LiveStream.')"/>
<script>
var theInput = document.getElementById("name");
</script>
</form>
<script>
newWindow.close();
</script>
Run Code Online (Sandbox Code Playgroud)
小智 6
另一种解决方案,除了明显的 ajax 之外,还使用内部隐藏的 iframe 作为表单元素的目标属性。这样,表单将在隐藏的内部元素中打开。例如:
<form id="eFrm" target="ifrm1">
</form>
<iframe id="ifrm1" name="ifrm1" style="display:none"></iframe>
Run Code Online (Sandbox Code Playgroud)