<script language="javascript">
function startTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
document.getElementById('time').innerHTML = "Current Time: " + h +
":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkForAnswers(){
alert('Hi');
if((document.f1.q1[0].checked==false)&&
(document.f1.q1[1].checked==false)&&
(document.f1.q1[2].checked==false)&&
(document.f1.q1[3].checked==false))
alert('Please attend all the questions!')
else if((document.f1.q2[0].checked==false)&&
(document.f1.q2[1].checked==false)&&
(document.f1.q2[2].checked==false)&&
(document.f1.q2[3].checked==false))
alert('Please attend all the questions!')
else
alert('Thanks');
}
</script>
<body onload="startTime()">
<p id="time"></p>
<form action="results.php" method="post" name="f1">
<p><b>Quiz</b></p>
<p>1. What is the capital of India?</p>
<input type="radio" name="q1" id="q1-A" value="A">Delhi<br/>
<input type="radio" name="q1" id="q1-B" value="B">Chennai<br/>
<input type="radio" name="q1" id="q1-C" value="C">Mumbai<br/>
<input type="radio" name="q1" id="q1-D" value="D">Kolkata<br/>
<p>2. What is the capital of USA?</p>
<input type="radio" name="q2" id="q2-A" value="A">Miami<br/>
<input type="radio" name="q2" id="q2-B" value="B">Houston<br/>
<input type="radio" name="q2" id="q2-C" value="C">Washington DC<br/>
<input type="radio" name="q2" id="q2-D" value="D">Palo Alto<br/>
<br/>
<input type="submit" value="Submit" id="button" onclick="checkForAnswers()">
</form>
</body>
我试图使用PHP创建一个简单的测验应用程序,并检查是否所有问题都得到了回答,我编写了一个名为checkForAnswers()
的函数,并在按钮的onClick()
上调用了它,可以工作(显示警报),但是它不会停留在同一页面上,而是继续显示results.php
,其中显示了PHP错误。
由于PHP,我也不能在此处使用按钮代替提交。
我现在该怎么办?请帮助我!
答案 0 :(得分:1)
您将函数分配给表单的onsubmit,并使函数返回false。仅当您要继续时才返回true
<form action="results.php" method="post" name="f1" onsubmit="return checkForAnswers()">
编辑:返回checkForAnswers()将起作用
答案 1 :(得分:1)
尝试这样的事情:
<script language="javascript">
function startTime(){
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
document.getElementById('time').innerHTML = "Current Time: " + h +
":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkForAnswers(e){
alert('Hi');
if(!document.f1.q1.value) {
alert('Please attend all the questions!');
return false;
}
else if(!document.f1.q2.value) {
alert('Please attend all the questions!')
return false;
}
alert('Thanks');
return true;
}
</script>
<body onload="startTime()">
<p id="time"></p>
<form action="results.php" method="post" name="f1" onsubmit="return checkForAnswers();">
<p><b>Quiz</b></p>
<p>1. What is the capital of India?</p>
<input type="radio" name="q1" id="q1-A" value="A">Delhi<br/>
<input type="radio" name="q1" id="q1-B" value="B">Chennai<br/>
<input type="radio" name="q1" id="q1-C" value="C">Mumbai<br/>
<input type="radio" name="q1" id="q1-D" value="D">Kolkata<br/>
<p>2. What is the capital of USA?</p>
<input type="radio" name="q2" id="q2-A" value="A">Miami<br/>
<input type="radio" name="q2" id="q2-B" value="B">Houston<br/>
<input type="radio" name="q2" id="q2-C" value="C">Washington DC<br/>
<input type="radio" name="q2" id="q2-D" value="D">Palo Alto<br/>
<br/>
<input type="submit" value="Submit" id="button">
</form>
</body>
它将在提交时运行您的功能(而不仅仅是按下按钮)。如果该函数返回false,它将停留在页面上,并警告错误,否则将被提交。