0

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

//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();
        }

}

一切都很好,除了原始窗口上的表单永远不会被提交。它将 material_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();
        }

}
4

1 回答 1

0

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

如果您要提交到同一个域,则始终可以使用 Ajax 进行第一次提交,而无需打开新窗口。

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

于 2012-10-30T16:22:43.763 回答