确认框功能未定义

时间:2015-03-23 15:45:25

标签: javascript validation confirm

只是想问一个简单的问题。在下面的代码中我已经完成了,我想知道你是否可以告诉我为什么在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;
}
}

谢谢大家,希望你能帮忙! ---- ---- EDIT

这是IE控制台错误的行号: SCRIPT5009:'check()'未定义 - 文件:Index.html,行:160,列:27

这是陷入困境的地方:

<input onClick="check()" type="radio" id="A1" name="examGroup" value="GCSE" />GCSE

2 个答案:

答案 0 :(得分:0)

因为check()是在函数validateForm()中定义的,因此它本身并没有真正定义。将其移出函数validateForm()以及其余函数。

function validateForm() {
    ...
};

function check() {
    ...
}

答案 1 :(得分:0)

我认为函数检查(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>