超时后在jquery php中提交表单?

时间:2016-10-13 06:22:42

标签: javascript php jquery

jquery表单提交功能在php中不起作用,我希望在超时后提交表单,但它不能正常工作我的代码如下。任何人都给我解决方案

<script>
    $( document ).ready(function() {
        var c=10;
        var t;
        timedCount();

        function timedCount()
        {
            var hours = parseInt( c / 3600 ) % 24;
            var minutes = parseInt( c / 60 ) % 60;
            var seconds = c % 60;
            var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
            $('#timer').html(result);
            if(c == 0 )
            {
                // form.submit();
                //$('#scoretarget').submit();
                //setConfirmUnload(false);
                document.formsubmit.submit();
                //alert("Your form  will be submited");
                //$( "#scoretarget" ).submit();
                // $('#scoretarget').submit();
                //$( "#target" ).submit();
                //window.location="logout.html";
            }else{
                c = c - 1;
                t = setTimeout(function(){
                        timedCount()
                    },1000);
            }
       }
    });
</script>

3 个答案:

答案 0 :(得分:0)

检查一下。

&#13;
&#13;
$( document ).ready(function() {
                var c=10;
                var t;
                timedCount();

                function timedCount()
                {

                    var hours = parseInt( c / 3600 ) % 24;
                    var minutes = parseInt( c / 60 ) % 60;
                    var seconds = c % 60;
                    var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
                    $('#timer').html(result);
                    if(c == 0 )
                    {
                        // form.submit();
                        //$('#scoretarget').submit();
                        //setConfirmUnload(false);
                        //document.formsubmit.submit();
                        alert("Your form  will be submited");
                        $("#scoretarget" ).submit();
                        // $('#scoretarget').submit();
                        //$( "#target" ).submit();
                        //window.location="logout.html";
                    }
                    else{
                        c = c - 1;
                        
                        t = setTimeout(function(){
                            timedCount()
                        },
                        1000);
                    }
                }
            });
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <form id="scoretarget" name="formsubmit" action="congrasulations.php" method="POST">
        </form>
    <div id="timer"></div>
&#13;
&#13;
&#13;

尝试添加jquery并使用$( "#scoretarget" ).submit();提交表单。

以下是整个HTML代码,而不是代码段。

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>

    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
            $( document ).ready(function() {
                var c=10;
                var t;
                timedCount();

                function timedCount()
                {

                    var hours = parseInt( c / 3600 ) % 24;
                    var minutes = parseInt( c / 60 ) % 60;
                    var seconds = c % 60;
                    var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
                    $('#timer').html(result);
                    if(c == 0 )
                    {
                        // form.submit();
                        //$('#scoretarget').submit();
                        //setConfirmUnload(false);
                        //document.formsubmit.submit();
                        alert("Your form  will be submited");
                        $("#scoretarget" ).submit();
                        // $('#scoretarget').submit();
                        //$( "#target" ).submit();
                        //window.location="logout.html";
                    }
                    else{
                        c = c - 1;

                        t = setTimeout(function(){
                            timedCount()
                        },
                        1000);
                    }
                }
            });
        </script>
        <form id="scoretarget" name="formsubmit" action="congrasulations.php" method="POST">
        </form>
        <div id="timer"></div>
    </body>

</html>

答案 1 :(得分:0)

使用以下代码提交表单,而不是document.formsubmit.submit();

    $('#formId')[0].submit();

并检查表单中是否存在名为“submit”的表单中的任何元素,如果有,请将元素的名称更改为其他名称,因为这可能会阻止您的表单提交

答案 2 :(得分:0)

1.这段代码

else{
    c = c - 1;
    t = setTimeout(function(){
        timedCount()
    }, 
    1000);
}

应该是:

else{
    t = setTimeout(function(){
        timedCount();
        c = c - 1;
    }, 
    1000);
}

以前,您的代码只会减少c一次。 您应该在c回调函数中递减setTimeout 内部,以便它会按超时值(即1000毫秒)递减。

2.在jQuery中触发表单提交,使用: $('form').trigger('submit');

来源:http://api.jquery.com/trigger/