确认框功能未定义
Confirm box function undefined
只是想问一个简短的问题。在我完成的下面的代码中显示,我想知道你是否可以告诉我为什么在 IE 控制台上它说函数名称未定义。
JavaScript代码:
function check() {
var checked = null;
var inputs = document.getElementsByName('examGroup');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checked = inputs[i];
break;
}
}
谢谢大家,希望对大家有所帮助!
----编辑----
这是带有行号的 IE 控制台错误:
SCRIPT5009:'check()' 未定义 - 文件:Index.html,行:160,列:27
这是卡住的地方:
<input onClick="check()" type="radio" id="A1" name="examGroup" value="GCSE" />GCSE
这是因为check()
是在函数validateForm()
中定义的,而在函数中并没有真正定义它自己。将它与其他函数一起移到函数 validateForm()
之外。
function validateForm() {
...
};
function check() {
...
}
我认为函数 check(this) 是在另一个函数 validateForm() 中定义的,因此没有定义。
所有函数都应该独立定义,简单的括号问题。
复制下面的代码试试:-
<script language="JavaScript" type="text/JavaScript">
/*
THINGS TO LOOK AT:
- ERROR TRAPPING OF CONFIRM
*/
function validateForm() {
var result = true;
var msg="";
document.getElementById('name').style.color="black";
document.getElementById('subject').style.color="black";
document.getElementById('CadNumber').style.color="black";
} //validateForm() ends here
function nameChecks() {
if (document.ExamEntry.name.value=="") {
msg+=("You must enter your name \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (!isNaN(parseInt(document.ExamEntry.name.value))) {
alert("You must only enter letters in the name! \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
} //nameChecks() ends here
function subjectChecks() {
if (document.ExamEntry.subject.value=="") {
msg+=("You must enter the subject \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (!isNaN(parseInt(document.ExamEntry.subject.value))) {
alert("Please make sure you only have letters in the subject! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('subject').style.color="red";
result = false;
}
} //subjectChecks() ends here.
function CadNumberChecks() {
if (document.ExamEntry.CadNumber.value=="") {
msg+=("You must enter the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value.length!== 4) {
alert("Make sure you only have 4 numbers in the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
if (isNaN(parseInt(document.ExamEntry.CadNumber.value))) {
alert("Please make sure you only have numbers in the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
} // CadNumberChecks ends here
function check() {
var checked = null;
var inputs = document.getElementsByName('examGroup');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checked = inputs[i];
break;
}
} // For loop ends here.
if(checked==null){
alert('Please choose an option.');
return false;
}
else
return confirm('You have chosen '+ checked.value + ', is this correct?');
nameChecks();
subjectChecks();
CadNumberChecks();
if (msg!="") {
}
alert(msg);
return result;
}
</script>
</head>
只是想问一个简短的问题。在我完成的下面的代码中显示,我想知道你是否可以告诉我为什么在 IE 控制台上它说函数名称未定义。
JavaScript代码:
function check() {
var checked = null;
var inputs = document.getElementsByName('examGroup');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checked = inputs[i];
break;
}
}
谢谢大家,希望对大家有所帮助! ----编辑----
这是带有行号的 IE 控制台错误: SCRIPT5009:'check()' 未定义 - 文件:Index.html,行:160,列:27
这是卡住的地方:
<input onClick="check()" type="radio" id="A1" name="examGroup" value="GCSE" />GCSE
这是因为check()
是在函数validateForm()
中定义的,而在函数中并没有真正定义它自己。将它与其他函数一起移到函数 validateForm()
之外。
function validateForm() {
...
};
function check() {
...
}
我认为函数 check(this) 是在另一个函数 validateForm() 中定义的,因此没有定义。
所有函数都应该独立定义,简单的括号问题。
复制下面的代码试试:-
<script language="JavaScript" type="text/JavaScript">
/*
THINGS TO LOOK AT:
- ERROR TRAPPING OF CONFIRM
*/
function validateForm() {
var result = true;
var msg="";
document.getElementById('name').style.color="black";
document.getElementById('subject').style.color="black";
document.getElementById('CadNumber').style.color="black";
} //validateForm() ends here
function nameChecks() {
if (document.ExamEntry.name.value=="") {
msg+=("You must enter your name \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (!isNaN(parseInt(document.ExamEntry.name.value))) {
alert("You must only enter letters in the name! \n");
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
} //nameChecks() ends here
function subjectChecks() {
if (document.ExamEntry.subject.value=="") {
msg+=("You must enter the subject \n");
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (!isNaN(parseInt(document.ExamEntry.subject.value))) {
alert("Please make sure you only have letters in the subject! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('subject').style.color="red";
result = false;
}
} //subjectChecks() ends here.
function CadNumberChecks() {
if (document.ExamEntry.CadNumber.value=="") {
msg+=("You must enter the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
if (document.ExamEntry.CadNumber.value.length!== 4) {
alert("Make sure you only have 4 numbers in the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
if (isNaN(parseInt(document.ExamEntry.CadNumber.value))) {
alert("Please make sure you only have numbers in the Candinate Number! \n");
document.ExamEntry.CadNumber.focus();
document.getElementById('CadNumber').style.color="red";
result = false;
}
} // CadNumberChecks ends here
function check() {
var checked = null;
var inputs = document.getElementsByName('examGroup');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checked = inputs[i];
break;
}
} // For loop ends here.
if(checked==null){
alert('Please choose an option.');
return false;
}
else
return confirm('You have chosen '+ checked.value + ', is this correct?');
nameChecks();
subjectChecks();
CadNumberChecks();
if (msg!="") {
}
alert(msg);
return result;
}
</script>
</head>