Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复选框表单javascript的验证_Javascript_Html_Validation_Dom - Fatal编程技术网

复选框表单javascript的验证

复选框表单javascript的验证,javascript,html,validation,dom,Javascript,Html,Validation,Dom,我是编程新手。我目前面临的问题是无法显示复选框问题的正确答案。问题编号为4,例如,选择问题4不会显示泰晤士河的正确答案。确保问题4是正确的 Javascript: myTimer = setTimeout('alert("Time out")', 600000); /*alert will appear after 60 second*/ function processMyForm() { var chosenans1 = document.mainForm.answer1.value; v

我是编程新手。我目前面临的问题是无法显示复选框问题的正确答案。问题编号为4,例如,选择问题4不会显示泰晤士河的正确答案。确保问题4是正确的

Javascript:

myTimer = setTimeout('alert("Time out")', 600000); /*alert will appear after 60 second*/

function processMyForm() {
var chosenans1 = document.mainForm.answer1.value;
var a1;

//alert(document.mainForm.answer1.value);
//alert(document.getElementById('answer2'));
var chosenans2 = document.getElementById('answer2').value;
var a2;



var chosenans4 = document.mainForm.answer4.checked;
var a4;




// ----------------------------

var total;

// --------------------------------

if (chosenans1 == "England") /*if radio value of answer1 is "England"*/ {
a1 = 2; /*correct answer for 2 marks*/
document.getElementById("result1").innerHTML = "<span class='correct'>Your answer to question 1 is correct.</span> "; /*print out correct at bottom of the page at <div id = "result1">*/
} else {
a1 = -1; /*incorrect answer for -1 mark*/
document.getElementById("result1").innerHTML = "<span class='incorrect'>The correct answer to question 1 is 'England'.</span>"; /*print out incorrect at <div id = "result1">*/
}

// ---------------------

if (chosenans2 == "Stonehenge") {
a2 = 2;
document.getElementById("result2").innerHTML = "<span class='correct'>Your answer to question 2 is correct.</span>";
} else {
a2 = -1;
document.getElementById("result2").innerHTML = "<span class='incorrect'>The correct answer to question 2 is 'Stonehenge'.</span>";
}

// -------------------

if (chosenans3 == "2012") {
a3 = 2;
document.getElementById("result3").innerHTML = "<span class='correct'>Your answer to question 3 is correct.</span>";
} else {
a3 = -1;
document.getElementById("result3").innerHTML = "<span class='incorrect'>The correct answer to question 3 is '2012'.</span>";
}

// ---------------------

if (chosenans4 == "River Thames") {
a4 = 2;
document.getElementById("result4").innerHTML = "<span class='correct'>Your answer to question 4 is correct.</span>";
} else {
a4 = -1;
document.getElementById("result4").innerHTML = "<span class='incorrect'>The correct answer to question 4 is 'River Thames'.</span>";
}



// --------------------------------------------------------

total = a1 + a2 + a3 + a4 ;//+ a5 + a6; /*add marks(2 or -1) together*/

document.getElementById("result").innerHTML = ("Your mark is " + total); //print out your total mark at <div id = "result">
alert("Your mark is " + total); //prompt total mark in small window

if (total < 4) {
document.body.style.backgroundImage = "none"; //remove background image
document.body.style.backgroundColor = "#bb0000"; //add a background colour
} else {
document.body.style.backgroundImage = "none";
document.body.style.backgroundColor = "#006600";
}

clearTimeout(myTimer); //stop timer
}


function getValue(qArray) { //get value from radio array
var i;
for (i = 0; i < qArray.length; i++) {
if (qArray[i].checked) return qArray[i].value;
}
return "";
}
myTimer=setTimeout('alert(“timeout”)”),600000/*警报将在60秒后出现*/
函数processMyForm(){
var chosenans1=document.mainForm.answer1.value;
变量a1;
//警报(document.mainForm.answer1.value);
//警报(document.getElementById('answer2'));
var chosenans2=document.getElementById('answer2').value;
VarA2;
var chosenans4=document.mainForm.answer4.checked;
var a4;
// ----------------------------
总风险价值;
// --------------------------------
如果(chosenans1==“英格兰”)/*如果answer1的无线电值为“英格兰”*/{
a1=2;/*答对2分*/
document.getElementById(“result1”).innerHTML=“您对问题1的回答是正确的。”;/*在页面底部的*/
}否则{
a1=-1;/*回答不正确,为-1分*/
document.getElementById(“result1”).innerHTML=“问题1的正确答案是‘英格兰’”;/*打印出的内容不正确*/
}
// ---------------------
如果(chosenans2==“巨石阵”){
a2=2;
document.getElementById(“result2”).innerHTML=“您对问题2的回答是正确的。”;
}否则{
a2=-1;
document.getElementById(“result2”).innerHTML=“问题2的正确答案是‘巨石阵’”;
}
// -------------------
如果(chosenans3==“2012”){
a3=2;
document.getElementById(“result3”).innerHTML=“您对问题3的回答是正确的。”;
}否则{
a3=-1;
document.getElementById(“result3”).innerHTML=“问题3的正确答案是‘2012’”;
}
// ---------------------
如果(chosenans4==“泰晤士河”){
a4=2;
document.getElementById(“result4”).innerHTML=“您对问题4的回答是正确的。”;
}否则{
a4=-1;
document.getElementById(“result4”).innerHTML=“问题4的正确答案是‘泰晤士河’”;
}
// --------------------------------------------------------
总计=a1+a2+a3+a4;//+a5+a6;/*同时添加标记(2或-1)*/
document.getElementById(“结果”).innerHTML=(“你的分数是”+total);//在
警告(“您的分数为”+总计);//在小窗口中提示总计分数
如果(总数<4){
document.body.style.backgroundImage=“无”//删除背景图像
document.body.style.backgroundColor=“#bb0000”//添加背景色
}否则{
document.body.style.backgroundImage=“无”;
document.body.style.backgroundColor=“#006600”;
}
clearTimeout(myTimer);//停止计时器
}
函数getValue(qArray){//从无线阵列获取值
var i;
对于(i=0;i
HTML:


1:伦敦属于:


英格兰
苏格兰
威尔士
北爱尔兰

2:以下哪项不在伦敦:


选择一个答案 圣保罗大教堂 白金汉宫 巨石阵


4:哪条河流经伦敦:


塞纳河
莱茵河
泰晤士河



假设您关闭了开口
, 看起来您需要添加“result”元素

innerHTML赋值的结果(例如:
document.getElementById(“result4”).innerHTML=…
)引发错误,因为没有具有
id=“result4”
的元素

在希望显示正确结果消息的位置添加如下内容:

<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<div id="result4"></div>
<div id="result5"></div>
<div id="result6"></div>
<div id="result"></div>

myTimer=setTimeout('alert(“timeout”)”),600000/*警报将在60秒后出现*/
函数processMyForm(){
var chosenans1=document.mainForm.answer1.value;
变量a1;
//警报(document.mainForm.answer1.value);
//警报(document.getElementById('answer2'));
var chosenans2=document.getElementById('answer2').value;
VarA2;
var chosenans3=document.mainForm.answer3.value;
var a3;
//警报(document.mainForm.answer3.value);
var chosenans4=document.mainForm.answer4.checked;
var a4;
var chosenans5=document.mainForm.answer5.value;
var a5;
var chosenans6=document.mainForm.answer6.value;
var a6;
// ----------------------------
总风险价值;
// --------------------------------
如果(chosenans1==“英格兰”)/*如果answer1的无线电值为“英格兰”*/{
a1=2;/*答对2分*/
document.getElementById(“result1”).innerHTML=“您对问题1的回答是正确的。”;/*在页面底部的*/
}否则{
a1=-1;/*回答不正确,为-1分*/
document.getElementById(“result1”).innerHTML=“问题1的正确答案是‘英格兰’”;/*打印出的内容不正确*/
}
// ---------------------
如果(chosenans2==“巨石阵”){
a2=2;
document.getElementById(“result2”).innerHTML=“您对问题2的回答是正确的。”;
}否则{
a2=-1;
document.getElementById(“result2”).innerHTML=“问题2的正确答案是‘巨石阵’”;
}
// -------------------
如果(chosenans3==“2012”){
a3=2;
document.getElementById(“result3”).innerHTML=“您对问题3的回答是正确的。”;
}否则{
a3=-1;
document.getElementById(“result3”).innerHTML=“问题3的正确答案是‘2012’”;
}
// ---------------------
如果(chosenans4==“泰晤士河”){
a4=2;
document.getElementById(“result4”).innerHTML=“您对问题4的回答是正确的。”;
}否则{
a4=-1;
document.getElementById(“result4”).innerHTML=“问题4的正确答案是‘泰晤士河’”;
}
// -------------------------
如果(chosenans5==“磅”){
a5=2;
document.getElementById(“result5”).innerHTML=(“您对问题5的回答是正确的”);
}否则{
a5=-1;
document.getElementById(“result5”).innerHTM
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<div id="result4"></div>
<div id="result5"></div>
<div id="result6"></div>
<div id="result"></div>
<input type="checkbox" id="correct" name="answer4" value="River Thames">River Thames<br>
var chosenans4 = document.getElementById('correct').checked;
if (chosenans4) {
//your code