表单不会按时自动提交 [英] Form does not submits automatically on time over

查看:61
本文介绍了表单不会按时自动提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个测验模块,如果该测验超过了时间限制(即30分钟),则必须自动提交测验.我已经为此使用了jQuery,但是当达到时间限制时,测验不会自动提交.我可以知道我要去哪里错了.任何对此的见解将非常有帮助.以下是我的代码段,

I am working on a quiz module where the quiz has to be submitted automatically if it crosses the time limit i.e. 30 mins. I have used jquery for this but somehow the quiz doesn't submits automatically when time limit reaches. may I know where am I going wrong. Any insight to this will be very helpful. Following is my code snippet,

function get15dayFromNow() {   

   return new Date(new Date().valueOf() + <?php echo $duration ; ?> * 60 * 1000);

       }

   var $time_spend = $('#time_spend');
  $time_spend.countdown(get15dayFromNow(), function(event) {
    $(this).val(event.strftime('%M:%S'));
  });

  var $clock = $('#clock');
  $clock.countdown(get15dayFromNow(), function(event) {
    $(this).html('Time Left :   '+'00:'+event.strftime('%M:%S'));

    var counterVal  =   $("#clock").text();
    if((counterVal   == '00:00') || (counterVal   == '00:00:00'))
    { 
        submitForm(); 
    }

 });

function submitForm()
{
    document.getElementById("target").submit();

}

 <div class="lead" style="width:30%; text-align:right; font-size:14px; font-weight:bold;" id="clock"><?php echo $duration ; ?>:00</div>
<form id="target" method="post" action="processQuiz_chapter.php">

//Question and options listings here.

 <button type="submit" style="display:none;"  name="submitQuiz" value="submit" class="btn btn-success wrp_submit"><i class="glyphicon glyphicon-floppy-disk"></i> Submit</button>
        <button type="submit" value="submit" class="btn btn-success wrp_submit_togle"><i class="glyphicon glyphicon-floppy-disk"></i> Submit</button>
</form>

推荐答案

首先,您需要将js代码保留在文档就绪功能中,

First of all you need to keep you js code in document ready function,

有一个事件将在您的时间结束时执行,您可以使用该事件 活动提交表格.以 finish.countdown

There is an event which will execute on completion of your time, you can use that event to submit the form. Look at the line having text as finish.countdown

尝试以下代码.

$(document).ready(function(){
    function get15dayFromNow() {   

       return new Date(new Date().valueOf() + <?php echo $duration ; ?> * 60 * 1000);

           }

       var $time_spend = $('#time_spend');
          $time_spend.countdown(get15dayFromNow(), function(event) {
            $(this).val(event.strftime('%M:%S'));
          });

       var $clock = $('#clock');
        $clock.countdown(get15dayFromNow(), function(event) {
            $(this).html('Time Left :   '+'00:'+event.strftime('%M:%S'));
        })
        .on('finish.countdown', function() {
            submitForm();
        });

    function submitForm()
    {
        document.getElementById("target").submit();

    }
});

有关文档

这篇关于表单不会按时自动提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆