簡體   English   中英

如何使用Java編寫兩次提交表單

[英]How to get a form to submit twice using 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();
        }

}

除了原始窗口上的表單永遠不會提交之外,其他所有功能都可以正常工作。 它將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();
        }

}

您沒有足夠的時間讓表單執行操作。 您需要分解請求。 使用setTimeout執行第二個操作。

如果您要提交到同一域,則始終可以使用Ajax進行第一次提交,而不必打開新窗口。

更好的是讓服務器處理請求並進行第二次提交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM