gpt4 book ai didi

窗口提交方法内的Javascript停止工作

转载 作者:行者123 更新时间:2023-12-02 22:57:21 26 4
gpt4 key购买 nike

我使用 window.open 方法来映射我的表单并将一些元素附加到窗口。

现在,一旦我这样做了,我就需要提交表格。我正在使用它来提交表单,一旦 window.open 工作,元素就被绑定(bind)了,我需要提交它。但不知怎的,它没有提交表格,我尝试了几种方法,但可惜我找不到一种。它今天在我的实时网站上停止运行了吗?

这是我的代码:

win = window.open("", "", "height=" + height + ",width=" + width +",status=yes")
if (win != null)
{
win.document.open('text/html')
win.document.write("<HTML><HEAD><TITLE>text Order Submission</TITLE></HEAD><BODY>")
win.document.write(content)
win.document.write("</BODY></HTML>")
win.document.close()
}

if (win) {
// Hide popblock.
document.getElementById('popblock').style.visibility = 'hidden';

// Copy over information, using getElementsByTagName if available, and old-style
// code if it isn't.
form = win.document.custform;
if (document.getElementsByTagName) {
elements = form.getElementsByTagName('input');
for( i=0; elm=elements.item(i); i++) {
if (elm.getAttribute('type') == "hidden") {
//alert("Setting " + elm.name + " to " + window.document.getElementById(elm.name).value);
elm.value = (window.document.getElementById(elm.name)) ? window.document.getElementById(elm.name).value : '';
}
}
}
else {
// Actually looking through more elements here but the result is the same.
elements = form.elements;
for( i=0; elm=elements[i]; i++) {
if (elm.type == "hidden") {
elm.value = (window.document.getElementById(elm.name)) ? window.document.getElementById(elm.name) : '';
}
}
}

// Submit custform to orderprep.php
win.document.custform.submit();

问题是我的元素确实被绑定(bind),但表单未提交,我做错了什么吗?

最佳答案

窗口正在异步加载,但您没有等待其 DOM 加载。您需要在窗口的 load 事件监听器中运行底部的代码。

let win = window.open("", "", "height=" + height + ",width=" + width + ",status=yes")
if (win) {
win.document.open('text/html')
win.document.write("<HTML><HEAD><TITLE>text Order Submission</TITLE></HEAD><BODY>")
win.document.write(content)
win.document.write("</BODY></HTML>")
win.document.close()
win.addEventListener("load", function() {
// Hide popblock.
document.getElementById('popblock').style.visibility = 'hidden';

// Copy over information, using getElementsByTagName if available, and old-style
// code if it isn't.
let form = win.document.custform;
let elm;
if (document.getElementsByTagName) {
let elements = form.getElementsByTagName('input');
for (let i = 0; elm = elements.item(i); i++) {
if (elm.getAttribute('type') == "hidden") {
//alert("Setting " + elm.name + " to " + window.document.getElementById(elm.name).value);
elm.value = (window.document.getElementById(elm.name)) ? window.document.getElementById(elm.name).value : '';
}
}
} else {
// Actually looking through more elements here but the result is the same.
let elements = form.elements;
for (let i = 0; elm = elements[i]; i++) {
if (elm.type == "hidden") {
elm.value = (window.document.getElementById(elm.name)) ? window.document.getElementById(elm.name) : '';
}
}
}

// Submit custform to orderprep.php
win.document.custform.submit()
});
}

您还应该记住将所有变量声明为局部变量。

关于窗口提交方法内的Javascript停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57930191/

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