首页 > 解决方案 > 时间到时如何调用提交按钮

问题描述

我想用javascript自动点击提交表单时遇到问题,所以在这种情况下,我想在时间到时提交按钮验证答案,我的代码有问题吗这里是我调用提交按钮的代码

document.getElementById('onResult').submit();

下面是完整的代码

<script type="text/javascript">
            var upgradeTime = @Model.Duration;
            var seconds = upgradeTime;
            function timer() {
                var days = Math.floor(seconds / 24 / 60 / 60);
                var hoursLeft = Math.floor((seconds) - (days * 86400));
                var hours = Math.floor(hoursLeft / 3600);
                var minutesLeft = Math.floor((hoursLeft) - (hours * 3600));
                var minutes = Math.floor(minutesLeft / 60);
                var remainingSeconds = seconds % 60;
                function pad(n) {
                    return (n < 10 ? "0" + n : n);
                }
                document.getElementById('countdown').innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(remainingSeconds);
                if (seconds == 0) {
                    clearInterval(countdownTimer);
                    //alert("Your Test Time Has Expire...!");]
                    document.getElementById('onResult').submit();
                    document.getElementById('countdown').innerHTML = "Completed";
                } else {
                    seconds--;
                }
            }
            var countdownTimer = setInterval('timer()', 1000);
    </script>
    <span id="countdown" class="timer"></span>
    @using (Html.BeginForm("CollectQuiz", "Exam"))
    {
        @Html.HiddenFor(model => @Model.Id, new { htmlAttributes = new { @class = "form-control" } })
        for (var i = 0; i < Model.Soals.Count(); i++)
        {
            <div class="modal-content">
                <div class="modal-body">
                    <p>@Model.Soals[i].Id @Model.Soals[i].Question</p>

                    @Html.HiddenFor(model => @Model.Soals[i].Id, new { htmlAttributes = new { @class = "form-control" } })

                    @for (var j = 0; j < Model.Soals[i].Choices.Count(); j++)
                    {
                        <p>
                            @Html.RadioButtonFor(m => m.Soals[i].SelectedAnswerId, Model.Soals[i].Choices[j].Id, new { id = "Question" + i.ToString() + "Answer" + j.ToString() })
                            <label for="Question@(i)Answer@(j)">@Model.Soals[i].Choices[j].Id @Model.Soals[i].Choices[j].Name</label>
                        </p>
                    }
                    <button class="btn btn-sm text-info"><b>Kesulitan:  @Model.Soals[i].Difficulty</b> </button>
                </div>
            </div>
        }
        <input id="onResult" type="submit" value="Validate Answers" />
    }

我收到错误 Document.getElementById(...).submit is not a function

标签: javascriptasp.net-mvc

解决方案


我不熟悉您在这里使用的任何类型的前端框架,但我知道这submit需要在表单元素上完成,<form>而不是在<input>. 我认为如果您将其更改为引用表单元素 ID,您会很高兴。

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit


推荐阅读