我遇到了两个函数和一个if语句的问题。我被告知函数go
和postcodeChange
未定义。
我也被告知flag
是if flag == 1
的意外标识符。
知道我哪里错了吗?谢谢。
function postcodeChange(){
document.getElementById("goButton").onclick = distanceCheck;
}
function distanceCheck(){
var distance = document.getElementById("distance").value
var patt1=new RegExp("^[0-9]+(\.[0-9]{1})?$");
var out = patt1.exec(distance);
if (out == null) {
//distance is not a valid number
document.getElementById("distanceFlag").value = 1
}
else {
//distance is valid number
document.getElementById("distanceFlag").value = 0
}
function go(){
var flag = document.getElementById("distanceFlag").value
if flag == 1
{
alert("Distance is not valid- enter a number with no more than one decimal point");
}
else{
popSubmit('#fa Care Provider Search Go','','0');
}
}
答案 0 :(得分:2)
未定义的函数是语法错误的直接后果 - 语法错误,函数无法理解,因此未定义。
所以,关于语法错误,我很确定if
条件的括号是强制性的:
if( flag == 1)
编辑:此外,正如Wouter指出的那样,您错过了}
来结束distanceCheck
功能定义。还有一件事,请用;
终止你的行,或准备好令人讨厌的惊喜。 JavaScript会让你失望!
答案 1 :(得分:0)
您忘了关闭distancecheck功能。
function postcodeChange(){
document.getElementById("goButton").onclick = distanceCheck;
}
function distanceCheck(){
var distance = document.getElementById("distance").value
var patt1=new RegExp("^[0-9]+(\.[0-9]{1})?$");
var out = patt1.exec(distance);
if (out == null) {
//distance is not a valid number
document.getElementById("distanceFlag").value = 1
}
else {
//distance is valid number
document.getElementById("distanceFlag").value = 0
}
}
function go(){
var flag = document.getElementById("distanceFlag").value
if flag == 1
{
alert("Distance is not valid- enter a number with no more than one decimal point");
}
else{
popSubmit('#fa Care Provider Search Go','','0');
}
}
答案 2 :(得分:0)
If语句不正确:
使用
if(flag==1){//your statement}
else {//your statement}
且distanceCheck()
的功能范围不正确。
答案 3 :(得分:0)
function postcodeChange(){
document.getElementById("goButton").onclick = distanceCheck;
}
function distanceCheck() {
var distance = document.getElementById("distance").value
var patt1=new RegExp("^[0-9]+(\.[0-9]{1})?$");
var out = patt1.exec(distance);
if (out == null) {
//distance is not a valid number
document.getElementById("distanceFlag").value = 1
}
else {
//distance is valid number
document.getElementById("distanceFlag").value = 0
}
// }
// should we close the function here ?
// Or is `go` a function inside the function `distanceCheck`:
function go(){
var flag = document.getElementById("distanceFlag").value
if ( flag == 1 ) { // <-- Use parantes' when dealing with if-statements
alert("Distance is not valid- enter a number with no more than one decimal point");
}
else {
popSubmit('#fa Care Provider Search Go','','0');
}
}
// Nether the less you will need to close the function scope somewhere if not above then down here:
}