javascript - 时间到时如何调用提交按钮
问题描述
我想用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
解决方案
我不熟悉您在这里使用的任何类型的前端框架,但我知道这submit
需要在表单元素上完成,<form>
而不是在<input>
. 我认为如果您将其更改为引用表单元素 ID,您会很高兴。
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit
推荐阅读
- sqlite - SQL查询:如何计算列中特定值的出现而不对该列进行分组?
- c - Microsoft Visual C++ 运行时检查失败 #2
- java - Java - 使用单独的方法计算随机生成器方法中字符的出现次数
- java - Jersey 删除 - id 包含斜杠 - URLEncode 不起作用
- spring-boot - Spring Boot 无法将 Redis 客户端 Jedis 连接到池:连接被拒绝
- javascript - 如何在 2D 空间 (x,y) 中制作 3D (X,Y,Z) 形状
- azure - 我可以在 BotBuilder 中使用 PubNub 而不是 DirectLine 吗?
- powershell - Powershell - 命令文本未出现
- python - Python中多维数组的非连续切片
- python - 如何在 scipy 稀疏 lil_matrix 中存储显式 0 值?