如何使用JavaScript获得两次表格



我需要向新窗口提交表单,然后在原始窗口中的同一表单提交稍微更改的值。我有以下功能来做到这一点。

//Now lets create the page making function!
function createInternetPage() {
//Start by checking to see if the internet page has been requested. 
req_int = document.getElementById("marterial_internet").checked;
    if (req_int == true){
        //If it has, create the internet page.
//Send the completed form to submit in a new window, creating the broadcast page. 
document.getElementById("new_material").setAttribute("target","_blank");
document.forms["new_material"].submit();
//Add "(Internet)" to the current form title
var title = document.getElementById("material_title").value;
var title_new = title + " (Internet)";
title = document.getElementById("material_title").value = title_new;
//Then submit the form on the existing window to make the internet page. 
document.getElementById("new_material").setAttribute("target","_self");
document.forms["new_material"].submit();
        }
        //If it has not been requested then just submit the normal form. 
        else {
            //alert("NOT Checked");
            document.getElementById("new_material").setAttribute("target","_self");
            document.forms["new_material"].submit();
        }
}

一切都很好,除了原始窗口上的表格永远不会提交。它更改了材料_title值以在其之后添加"(Internet)",但不提交表格。

有什么想法是为什么这是为了使此工作的工作吗?

编辑:添加settimeout延迟时,请参见下文,正在发生同样的事情。除了最后一个表单提交外,一切都运行。

function delay() {
    //Send the completed form to submit in a new window, creating the broadcast page. 
document.getElementById("new_material").setAttribute("target","_blank");
document.forms["new_material"].submit();
}
function delay2(){
    var title = document.getElementById("material_title").value;
var title_new = title + " (Internet)";
title = document.getElementById("material_title").value = title_new;
//Then submit the form on the existing window to make the internet page. 
document.getElementById("new_material").setAttribute("target","_self");
document.forms["new_material"].submit();
}
//Now lets create the page making function!
function createInternetPage() {
//Start by checking to see if the internet page has been requested. 
req_int = document.getElementById("marterial_internet").checked;
    if (req_int == true){
        //If it has, create the internet page.
delay()
//Add "(Internet)" to the current form title
setTimeout('delay2()',10000);
        }
        //If it has not been requested then just submit the normal form. 
        else {
            //alert("NOT Checked");
            document.getElementById("new_material").setAttribute("target","_self");
            document.forms["new_material"].submit();
        }
}

您没有给出足够的时间来执行操作。您需要分解请求。使用settimeout执行第二个动作。

如果要提交同一域,则可以随时使用AJAX进行第一次提交而不打开新窗口。

更好,是让服务器处理请求并进行第二次提交。

最新更新