我是javascript新手,我是自学在线家庭教程。 我的Javacript教程要求我尝试基本的表单代码。
但功能checkRadio()未发布错误消息!(如果单选按钮未选中 - 则发布错误消息)
功能checkEmail()会正确发布错误消息。
我已多次检查语法。你能发现错误吗?
谢谢
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Javascript and Forms</TITLE>
<SCRIPT LANGUAGE = "Javascript">
function validate() {
var emailError = checkEmail();
var radioError = checkRadio();
var dropdownError = checkDropdown();
var checkboxError = checkCheckbox();
}
function checkEmail(){
var email = document.frmOne.email.value;
if(email==""){
document.getElementById("email_error").innerHTML = "No Email Adress";
return false;
}
else{
document.getElementById("email_error").innerHTML = "";
return true;
}
}
function checkRadio(){
var payment = "";
var len = document.frmone.payment.length;
var i;
for(i = 0;i<len;i++){
if (document.frmone.payment[i].checked){
payment = document.frmone.payment[i].value;
break;
}
}
if(payment == ""){
document.getElementById("radio_error").innerHTML = "No payment Option Selected";
return false;
}
else{
document.getElementById("radio_error").innerHTML = "";
return true;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="frmOne" ACTION="" METHOD="POST">
<P>
EMAIL: <INPUT TYPE="text" NAME="email" VALUE="" PLACEHOLDER="Email Address">
<SPAN STYLE=color:red ID="email_error"></SPAN>
</P>
<P>
Payment Options:
<INPUT TYPE="Radio" Name="payment" Value="CC">Credit Card
<INPUT TYPE="Radio" Name="payment" Value="DC">Debit Card
<INPUT TYPE="Radio" Name="payment" Value="PP">PayPal
<SPAN STYLE=color:red ID="radio_error"></SPAN>
</P>
<P>
Where did you hear about us?
<SELECT NAME="hear">
<OPTION Value="None">None</OPTION>
<OPTION Value="paper">Newspaper</OPTION>
<OPTION Value="tv">Television</OPTION>
<OPTION Value="mouth">Word of Mouth</OPTION>
<OPTION Value="seach">Search Engine</OPTION>
</SELECT>
<SPAN STYLE=color:red ID="dropdown_error"></SPAN>
</P>
<P>
<LABEL FOR="CB1">Terms and Conditions</LABEL>
<INPUT TYPE="Checkbox" Name="terms" ID="CB1" Value="TAC">
<SPAN STYLE=color:red ID="checkbox_error"></SPAN>
</P>
<P> </P>
<INPUT TYPE="button" VALUE=" SUBMIT " onClick="validate()">
</FORM>
</BODY>
</HTML>
答案 0 :(得分:1)
你错过了这一行的大写字母:
len = document.frmone.payment.length;
应为len = document.frmOne.payment.length;
您应该学会使用浏览器的网络检查器,它可以帮助您轻松发现这些内容。
答案 1 :(得分:1)
有3个地方需要将frmone
更改为frmOne
。一个是马可指出的。其他地方是
if (document.frmOne.payment[i].checked){
payment = document.frmOne.payment[i].value;
break;
}