gpt4 book ai didi

javascript - 表单自动提交后如何关闭Html窗口

转载 作者:行者123 更新时间:2023-11-28 04:24:58 24 4
gpt4 key购买 nike

我们有两个 html 页面。第一页我们使用 window.open("./pop.html", "",'width=200,height=100'); 打开第二页

pop.html我们会自动提交表单,提交后需要关闭 HTML。代码如下。

<html>
<head>
<script type="text/javascript">

function loadForm() {
var method = "post"; // Set method to post by default if not specified.
var path = "http://localhost:8080";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);

var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "username");
hiddenField.setAttribute("value", "sai");

var passField = document.createElement("input");
passField.setAttribute("type", "hidden");
passField.setAttribute("name", "password");
passField.setAttribute("value", "sai123");

form.appendChild(hiddenField);
form.appendChild(passField);

document.body.appendChild(form);
form.submit();

window.close();
}

</script>
</head>

<body onload="loadForm();">
<form>
</form>
</body>
</html>

但是window.close();无法在页面中工作。是不是少了什么东西?

最佳答案

通过窗口名称打开和关闭窗口。示例:

打开窗口:

var my_window = window.open("URL_HERE", "my_window", "height=100,width=100");

关闭窗口:

my_window.close();

或者在 my_window 的内容中:

window.close();

关于javascript - 表单自动提交后如何关闭Html窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45141192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com