确认框功能未定义

Confirm box function undefined

本文关键字:未定义 功能 确认      更新时间:2023-09-26

我只是想问一个简短的问题。在我所做的下面的代码中,我想知道你是否可以告诉我为什么在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>