douyun1852 2015-11-13 17:25
浏览 107
已采纳

使用javascript在PHP中定时器达到零时提交方法未被调用

Im creating a sample quiz webpage using codeigniter. And in my one of the views there is a scenario where there is a timer and when it reaches zero the form should be submitted. I have tried a code and the timer works but when it reaches zero the call to submit is not being done, so im unable to navigate to the next page. But when submit button is clicked with works fine. Below is my code. Can anyone help me find where im going wrong.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test Your Knowledge</title>

        <script type="text/javascript">
            var totalsec = 50;
            var c_min = parseInt(totalsec / 60);
            var c_sec = parseInt(totalsec % 60);

            function CheckTime() {
                document.getElementById("timer").innerHTML = "Time left: " + c_min + " min " + c_sec + " sec ";

                if (totalsec <= 0) {
                    document.getElementById("difficultyForm").submit();
                } else {
                    totalsec = totalsec - 1;
                    c_min = parseInt(totalsec / 60);
                    c_sec = parseInt(totalsec % 60);
                    setTimeout("CheckTime()", 1000);
                }
            }
            setTimeout("CheckTime()", 1000);
        </script>

        <style type="text/css">

        </style>
    </head>
    <body>

        <div id="container">
            <h1>Welcome to Test Your Knowledge!!!</h1>

            <div id="body">
                <h2>Selected the difficulty level: Easy</h2>
                <div><span id="timer"></span></div>
                <form action="/AWT_CW1/index.php/ResultController/" method="POST" id="difficultyForm" name="difficultyForm">
                    <?php
                    foreach ($quesAnsList as $quesAns) {
                        echo '<question>';

                        echo '<strong>' . $quesAns->question . '</strong>';
                        echo '<br>';

                        echo '<input type="radio" name="' . $quesAns->answer_id . '" value="' . $quesAns->answer1 . '">' . $quesAns->answer1 . ' <br>';
                        echo '<input type="radio" name="' . $quesAns->answer_id . '" value="' . $quesAns->answer2 . '">' . $quesAns->answer2 . ' <br>';
                        echo '<input type="radio" name="' . $quesAns->answer_id . '" value="' . $quesAns->answer3 . '">' . $quesAns->answer3 . ' <br>';

                        echo '</question>';
                    }
                    ?>
                    <input type="submit" name="submit" value="Submit">
                </form>
            </div>
        </div>
    </body>
</html>

Note: I was asked not to use jquery or ajax for this project. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dqwh1208 2015-11-13 17:37
    关注

    Why your code is not working

    That's because you named your submit button "submit", overriding the submit() function.

    How you can fix the problem

    You can fix it by replacing

    <input type="submit" name="submit" value="Submit">
    

    With

    <input type="submit" name="submit_button" value="Submit">
    

    Please keep in mind that...

    As @Rajesh pointed out in the comments, setTimeout expects a function as first argument, while you're providing a string. Replace setTimeout("CheckTime()", 1000); with

    setTimeout(function(){
      CheckTime();
    }, 1000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测