javascript - window.location.href 不提交表单
问题描述
我使用了下面的 Javascript。如果时间结束,它必须通过提交表单重定向到下一页“result.php”。但不幸的是,显示的页面是空的。在我的表格下方,必须发送到下一页“result.php”。我想知道如何发送此表单并重定向到下一页。如何使用此 Javascript 发送表单以使我的页面正常工作?
var hoursleft = 3;
var minutesleft = 0;
var secondsleft = 0;
var end1;
var progressBar = document.getElementById('progressBar');
if (localStorage.getItem("end1")) {
end1 = new Date(localStorage.getItem("end1"));
} else {
end1 = new Date();
end1.setMinutes(end1.getMinutes() + minutesleft);
end1.setSeconds(end1.getSeconds() + secondsleft);
end1.setHours(end1.getHours() + hoursleft);
}
progressBar.max = end1 - new Date();
var counter = function() {
var now = new Date();
var diff = end1 - now;
diff = new Date(diff);
var sec = parseInt((diff / 1000) % 60)
var mins = parseInt((diff / (1000 * 60)) % 60)
var hours = parseInt((diff / (1000 * 60 * 60)) % 24);
if (hours < 10) {
hours = "0" + hours;
}
if (mins < 10) {
mins = "0" + mins;
}
if (sec < 10) {
sec = "0" + sec;
}
if (now >= end1) {
clearTimeout(interval);
localStorage.setItem("end", null);
localStorage.removeItem("end1");
localStorage.clear();
if (confirm("TIME UP!"))
window.location.href = "result.php";
//alert("Time finished!");
} else {
progressBar.value = progressBar.max - (end1 - now);
localStorage.setItem("end1", end1);
document.getElementById('hours').innerHTML = "<b>" + hours + "</b> " + ":";
document.getElementById('mins').innerHTML = "<b>" + mins + "</b> " + ":";
document.getElementById('sec').innerHTML = "<b>" + sec + "</b>";
}
}
var interval = setInterval(counter, 1000);
<form class="form-horizontal" role="form" id='question' name="question" method="post" action="result.php">
<?php
$number_question=1;
$limit=10;
$row = mysqli_query( $conn, "select id,question_name,image from testquestion where email = '".$email."' ");
$total = mysqli_num_rows( $row );
$remainder = $total/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i != 0)
echo "<div id='question_splitter_$j'>";
if ( $i == 0)
echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?>
<input type="checkbox" id="review" name="review" />
<label style="color:#0080ff">Review</label></p>
</br>
<?php echo '<img src="data:image/png;base64,'.($result['image']).'" />'; ?></br><br/><br/>
<input type="text" id="answer<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" placeholder="Enter your answer" />
<br/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $total > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-primary' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $total ) {
echo " <button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<a href='review.php'><button id='review.php' class='previous btn btn-primary' type='button'>Review</button></a>
<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-primary' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
}
?>
<br/>
<br/>
<br/>
</form>
解决方案
您必须使用document.getElementById('question').submit();
未window.location.href = "result.php";
发布的 POST 数据
推荐阅读
- azure - 如何在 Kubernetes Yaml 文件中将 Pod 的限制取消设置为 Unlimited?
- php - 如果添加 Image(),fpdf 输出(名称)不起作用
- java - 不执行数学运算
- jquery - 为什么 attr('r') / attr('cx') / attr('cy') 都可以工作,但 attr('stroke') 不行?
- lucene.net - 字段查询方法在 Lucene.Net.highlighter 4.8.0 中不可用?无法将 FieldQuery 参数传递给 Get Fragment 方法?
- javascript - 在反应中映射数组元素时如何解决类型错误?
- python - Json 响应未将提供的用户写入 csv 文件
- javascript - 当 onchange 起作用时,onmouseover 不起作用?
- python - 有没有办法在 Python 3.9 类型提示中定义具有特定元素类型的列表数据结构?
- mongodb - 我们可以为 EC2 实例内的服务启用警报吗?