javascript - Javascript 警告框未显示和错误:您要求的文件不存在
问题描述
我有几个月来一直在研究的这段 Javascript 代码。我的目标是在用户没有选择或输入答案时显示一个警告框并警告用户。
目前,我有两个问题发生。一个问题是警报框根本没有出现。另一个是有时我在新页面上收到此错误说:错误您要求的文件不存在“。不知道我哪里出错了。
<!DOCTYPE html>
<html>
<body>
<form name="quiz" class="quiz" onsubmit="return submitQuiz();">
<ul style="list-style-type:none;">
<li><label id="web">Do you ever think about how you would design a web page?</label></li>
<br>
<li><input type="radio" value="no" id="rad1" name="rad1"/><span>No</span></li>
<li><input type="radio" value="yes" id="rad1" name="rad1"/><span>Yes</span></li>
<br>
<br>
<li><label for="prior">Which the following are your main priorities? If none, select N/A</label></li>
<li><select name="prior">
<option selected="" value="">**Please select one of the following**</option>
<option name="op1" id="op1">Ease of Use</option>
<option name="op2" id="op2">Graphics & Content</option>
<option name="op3" id="op3">The Data Collected</option>
<option name="op4" id="op4">Securing the site from possible attacks</option>
<option name="op5" id="op5">Overseeing the creation of software</option>
</select>
</li>
<br>
<br>
<li><label id="res">Do you enjoy conducting research, asking questions, and building reports?</label></li>
<br>
<li><input type="radio" value="no" id="rad2" name="rad2"/><span>No</span></li>
<li><input type="radio" value="yes" id="rad2" name="rad2"/><span>Yes</span></li>
<br>
<br>
<li><label for="tx1">Does hacking a system or stopping a system from being hacked sound interesting to you? Type Yes or No:</label></li>
<li><textarea name="tx1" id="text1" maxlength="3"></textarea></li>
<br>
<li><input type="submit" value="Submit!" id="submit"></li>
<li><input type="reset" id="reset"></li>
</ul>
</form>
<script>
function submitQuiz() {
"use strict";
var radio1 = document.quiz.rad1;
var radio2 = document.quiz.rad2;
var ch1 = document.quiz.op1;
var ch2 = document.quiz.op2;
var ch3 = document.quiz.op3;
var ch4 = document.quiz.op4;
var ch5 = document.quiz.op5;
var tx1 = document.quiz.text1;
function answerScore (radio1, radio2, radio3, radio4) {
var x = 0;
//inserted missing braces
if (radio1.checked && radio2.checked) {
x + 1;
}
if (x === 0) {
alert("You forgot a question!");
radio1.focus();
return false;
}
else if (x === 1) {
alert("Completed!");
window.location.reload();
return true;
}
}
function vCheck(ch1, ch2, ch3, ch4, ch5) {
var y = 0;
//inserted missing braces
if (ch1.checked || ch2.checked ||ch3.checked || ch4.checked || ch5.checked) {
y++;
}
if (y === 0) {
alert("You forgot a question!");
radio1.focus();
return false;
} else {
alert("Completed!");
window.location.reload();
return true;
}
}
function vLength(tx1) {
var txLength = tx1.value.length;
if (txLength === 0 || txLength < 3) {
alert("That is an incorrect entry, try again.");
tx1.focus();
return false;
} else {
return true;
}
}
function vCheck2(tx1) {
if ((tx1 === "Yes" || tx1 === "YES" || tx1 === "yes") && (tx1 === "No" || tx1 === "NO" || tx1 === "no")) {
tx1.focus();
return true;
} else {
alert("Uhoh, you're missing an answer!");
txt1.focus();
return false;
}
}
}
</script>
</body>
</html>
解决方案
这就是表单可以被验证和提交的方式。
function submitQuiz(myform) {
"use strict";
var errMsg = [];
var hasError = false;
if (!myform.rad1.value) {
errMsg.push('Do you ever think...');
myform.rad1[0].focus();//focus 1st option
hasError = true;
}
if (myform.prior.value === 'none') {
errMsg.push('Select main priopity');
hasError || myform.prior.focus();
hasError = true;
}
if (!myform.rad2.value) {
errMsg.push('Do you enjoy...');
hasError || myform.rad2[0].focus();
hasError = true;
}
if (myform.tx1.value.toLowerCase() !== 'yes' && myform.tx1.value.toLowerCase() !== 'no') {
errMsg.push('Type \'yes\' or \'no\'');
hasError || myform.tx1.focus();
hasError = true;
}
if (hasError) {
alert(errMsg.join('\n'));
return false;
}
//return false for debug purpose. In production return true
alert('Submitting form');
// return false;
}
<form name="quiz" method="get" class="quiz" onsubmit="return submitQuiz(this);">
<ul style="list-style-type:none;">
<li><label id="web">Do you ever think about how you would design a web page?</label></li>
<br>
<li><input type="radio" value="no" name="rad1" /><span>No</span></li>
<li><input type="radio" value="yes" name="rad1" /><span>Yes</span></li>
<br>
<br>
<li><label for="prior">Which the following are your main priorities? If none, select N/A</label></li>
<li>
<select name="prior">
<option value="none">**Please select one of the following**</option>
<option>Ease of Use</option>
<option>Graphics & Content</option>
<option>The Data Collected</option>
<option>Securing the site from possible attacks</option>
<option>Overseeing the creation of software</option>
</select>
</li>
<br>
<br>
<li><label id="res">Do you enjoy conducting research, asking questions, and building reports?</label></li>
<br>
<li><input type="radio" value="no" name="rad2" /><span>No</span></li>
<li><input type="radio" value="yes" name="rad2" /><span>Yes</span></li>
<br>
<br>
<li><label for="tx1">Does hacking a system or stopping a system from being hacked sound interesting to you? Type Yes or No:</label></li>
<!-- maxlength="3" not supported by textarea -->
<li><textarea name="tx1"></textarea></li>
<br>
<li><input type="submit" value="Submit!" id="submit"></li>
<li><input type="reset" id="reset"></li>
</ul>
</form>
推荐阅读
- c++ - 您可以从 c++2a 中的函数返回基于范围的视图吗?
- javascript - 如何使用保存按钮保存 textarea 值?
- numpy - 如何仅使用基本的 numpy 操作有效地计算两个数组的行之间的 L2 距离?
- javafx - 在 JavaFX Scene Builder 2.0 中导入 JFoenix 会使应用程序崩溃
- python - 在 Visual Studio Code 中保持 Python 函数的调试运行之间的状态
- c# - 如何在 WPF C# 中覆盖(使用)BitmapFrame.Thumbnail 属性?
- git - 尝试移动代码时出现 git blame -M -C 的问题
- functional-programming - 使参数可用于 Ramda 管道函数中的所有函数
- go - 缓冲区结束和超时
- reactjs - 如何将值输入到 rc-slider 句柄