ASP.NET MVC5,使用javascript为测验创建计时器

时间:2014-08-02 17:41:14

标签: javascript asp.net html.beginform

这是我使用的代码,我无法弄清楚为什么在0秒时,表单将不会提交到感谢页面。我试图从@using (htmlbeginform())语句中调用提交按钮。请帮忙。

<form name="counter">
    <input type="text" size="8"
           name="d2">
</form>


<script type="text/javascript">
    var minutes = 1
    var seconds = 00

    document.counter.d2.value = '30:00'

    function display()
    {
        if (seconds <= 0)
        {
            minutes -= 1
            seconds += 59
        }
        if (minutes <= -1)
        **{
            $(document).ready(function () {
                setTimeout(function () { nextQuestion() }, 5000);
                $("#questionform").submit(function () {
                    alert("Submit after 5 second.");
                });
            });
            function nextQuestion() {
                $("#questionform").trigger("submit");
            }

        }**  **PART of code in between ** is where I am having trouble, cant figure out what is wrong**

        else
            seconds -= 1
        document.counter.d2.value = minutes + ":" + seconds
        setTimeout("display()", 1000)
    }
    display()
</script>





@using (Html.BeginForm("ThankYou", "Questions", FormMethod.Post, new { @id = "questionform" }))
{
    @Html.EditorFor(x => x.Questions)

    <input type="submit" id="submit" class="btn btn-default" value="Submit" />
}

尝试在此处使用此按钮提交ZERO SECONDS

1 个答案:

答案 0 :(得分:0)

$(document).ready()事件仅在文档完全加载时触发, 由于您在加载文档后附加此事件,因此永远不会触发。

你必须这样做:

function nextQuestion() {
            $("#questionform").trigger("submit");
        } //it's better declare your functions outside an if statment

if (minutes <= -1) {           
            setTimeout(function () { nextQuestion() }, 5000);
            $("#questionform").submit(function () {
                alert("Submit after 5 second.");
            }); 
            /*if you submit, 
            the page changes and this alert will never appear*/
        });            
    }