0

I've written a code which displays the list of quizzes and when one is selected a new window is popped up and the online quiz runs. 问题是,当窗口关闭时,计时器应该到期,即表单应该在窗口关闭之前提交。生成新窗口的代码如下:

 var radios = document.getElementsByName("quizname");
var flag=0;
for (var i = 0; i < radios.length; i++) 
{       
    if (radios[i].checked) 
    {
        flag=1;
        var x=radios[i].value;
        var z=radios[i].value;
        break;
    }
}
if(flag==0)
{
    alert("Choose an option!!");
    exit;
}
var x="http://127.0.0.1/example/timedrunquiz.php?y="+x;
var win=window.open(x,z,"fullscreen=yes,scrollbars=yes");
win.focus();

我用过win.onbeforeunload,但我无法完成任务。我应该怎么做才能在关闭前提交表格?提前致谢!

4

2 回答 2

0

不确定这是否是正确的方法。您应该在窗口上有一个提交按钮,并带有一个可以win.close();在提交完成后调用的侦听器。

win.onbeforeunload现在,为了确保用户确实提交了任何内容,如果用户真的想关闭页面,您应该使用请求确认,如下所示:

win.onbeforeunload = function() {
    return confirm('Do you really want to exit? All your progress would be lost');
}
于 2013-09-12T22:06:40.107 回答
0

如果您想在关闭前提交表单,您可以使用 ajx 提交表单

 function callback()
    {
        $.ajax({
                   type: "POST",
                   url: "yourUrl",
                   data: 'YourData',


         });
        return "You are about to leave this page"; 
    }

    win.onbeforeunload = callBack;
于 2013-09-13T06:17:35.753 回答