簡體   English   中英

如果滿足條件,則自動單擊“提交”按鈕

[英]click submit button automatically if a condition is satisfied

我正在用PHP開發一個在線考試系統。這是我龐大的考試代碼的一部分..一切正常。時間到了,頁面將重定向到結果頁面

window.location = "http://localhost/result.php";//which is in comment line

但此處的答案值未發布。 但是,當我單擊提交按鈕時,它可以工作。 所以我做了一個功能來自動提交表單。但它不起作用,不知道為什么。 此處的“ if”塊正在檢查時間是否到了。 如果向上,請執行以下操作。 在“如果”內,我想單擊“提交按鈕”

 echo '<input type="submit" name="submit" id="submit" value="SUBMIT"/></FORM>'; 

或提交表格,以便我可以在下一頁“ result.php”中獲得結果。

<FORM name ='form1' id='form1' method ='post' action='result.php' >

提前致謝。 希望你理解我的問題。

function myfunc () {
    var frm = document.getElementById("form1");
    frm.submit();
}

document.getElementById("remain").innerHTML = hours+" hours : "+minutes+" minutes : "+seconds+" seconds";
SD=window.setTimeout( "setCountDown()", 1000 );
if (minutes == '00' && seconds == '00') { seconds = "00"; window.clearTimeout(SD);
    window.alert("Time is up. Press OK to continue.");
    window.onload = myfunc;
    //document.form1.submit();
    //window.location = "http://localhost/result.php"
}

嘗試這個..

window.onload = function(){
    myfunc();
};

如果您嘗試在window.alert("Time is up. Press OK to continue.")顯示之后提交表單,則不需要加載事件。 您只需將代碼放在警報后,它就會在警報關閉后立即運行(通過單擊“確定”):

alert("Time is up. Press OK to continue.");
myfunc();

或者,您可以使用confirm而不是alert

var timesUp = confirm("Time is up. Press OK to continue.");

if (timesUp) {
    myfunc();
}

進行confirm您將獲得兩個按鈕,並可以針對ok (true)和cancel (false)運行不同的事件。 這些選項之一應該可以解決您的問題。

只需使用此document.form1.submit.click();

暫無
暫無

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

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