当计时器倒计时到零时,表单未提交

时间:2017-05-02 11:00:26

标签: javascript php

我正在使用带有倒数计时器的表格。当计时器变为零时,表单应自动提交。但提交没有成功完成。当计时器到达' 0' 0它就停止了。我在这里给我的代码。

请找到我所犯的错误。

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="jquery.min.js"></script>
        <script>
            $(document).ready(function(){

                $("input[type='checkbox']").change(function() {
                    if ($(this).is(":checked"))
                    {
                        $(this).closest("thead").addClass("redBackground"); 
                    }
                    else
                    {
                        $(this).closest('thead').removeClass("redBackground");  
                    }
                });
            });
        </script>
        <script language="javascript">
            function over() {
                document.getElementById("submit").disabled = true;      
            }

            function time() {
                var minute = Math.floor(timeleft / 60);
                var seconds = timeleft % 60;
                if (timeleft <= 0)
                {
                    clearTimeout(timer);
                    document.getElementById("submit").submit();        
                }
                else
                {
                    if (minute < 10)
                    {
                        minute = "0" + minute;
                    }

                    if(seconds < 10)
                    {
                        seconds = "0" + seconds;
                    }
                    document.getElementById("timer").innerHTML = minute + ":" + seconds;
                }

                timeleft--;
                var timer = setTimeout(function(){time()}, 1000);
            }
        </script>
    <style>
    </head>
    <body onload="time()">
        <script>
            var timeleft = 2 * 60;
        </script>
        <?php
            include('connect.php');
            session_start();
            $sql = "select * from test";
            $query = mysql_query($sql);
            $i = 0;
            echo "<div id='timer'></div>";
            echo"<form method='post' action='test2.php' name='exam' id='exam'>";
            while($row = mysql_fetch_array($query))
            {  
                echo "<center><table border='0'>";
                echo "<thead>";
                $i++;
                echo "<td colspan='4'>";
                echo "<input type='checkbox' id='$i' name='$i'>";
                echo $i . ".";
                echo "&nbsp";
                echo $row['question'];  
                echo "</td>";
                echo "</thead>";
                echo "<tr>";
                echo "<td class='ans'>";
                echo "<input type='radio' name='ans$i' value='A'>";
                echo "(A)&nbsp&nbsp";
                echo $row['opt1'];
                echo "</td>";
                echo "<td class='ans'>";
                echo "<input type='radio' name='ans$i' value='B'>";
                echo "(B)&nbsp&nbsp";
                echo $row['opt2'];
                echo "</td>";
                echo "<td class='ans'>";
                echo "<input type='radio' name='ans$i' value='C'>";
                echo "(C)&nbsp&nbsp";
                echo $row['opt3']; 
                echo "</td>"; 
                echo "<td class='ans'>";  
                echo "<input type='radio' name='ans$i' value='D'>";
                echo "(D)&nbsp&nbsp";
                echo $row['opt4'];
                echo "</td>";
                echo "</tr>";
                echo "</table></center>";
                echo "<br>";   
            }

            echo "<center><input type='submit' name='submit' value='submit' id='submit' onclick='over();'></center>";
            echo "</form>";
            $_SESSION["qs"] = $i;
        ?>
    </body>
</html>

3 个答案:

答案 0 :(得分:0)

尝试以下代码:

 document.getElementById('submit').click();
 document.getElementById('submit').trigger('click');

答案 1 :(得分:0)

使用submit()的{​​{1}}方法,而不是尝试在提交按钮上调用它。

<form>

以下内容应该有效。问题似乎是您为提交按钮提供了// 'exam' is the ID of your form document.getElementById('exam').submit(); 的ID,该ID会覆盖submit的{​​{1}}方法。所以我将按钮重命名为submit(),然后就可以了。

&#13;
&#13;
form
&#13;
submitButton
&#13;
&#13;
&#13;

答案 2 :(得分:0)

写了一个示例代码,用于显示每秒减少的倒数计时器。最后,计时器将来到&#34; 0&#34;它调用一个函数sendFormDataToServer(),可用于进行其余的计算。

检查一次。也许它会解决你的问题。

<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

<script>
$(document).ready(function(){
    var countdown_limit = 5;

    // print the countdown message
    function showCountDownMsg(countdown_limit){
        $('#countdown').html(countdown_limit+' Sec Left');
    }

    showCountDownMsg(countdown_limit);

    // Refresh countdown timer in every 1sec(1000ms)
    var refreshInterval =setInterval(function(){ refreshCountDown(); }, 1000);

    // function for refresh countdown timer
    function refreshCountDown(){
        console.log('all is well');
        countdown_limit--;
        showCountDownMsg(countdown_limit);

        if(countdown_limit === 0){
            console.log("Countdown stopped");
            clearInterval(refreshInterval);

            // Call function to send form data to server
            sendFormDataToServer();
        }
    }

    function sendFormDataToServer(){
        console.log('EXtracting form data & Sending those to server ');
    }
});
</script>

</head>
<body>
    <div id='countdown'></div>
</body>
</html>