0

我遇到了两个函数和一个 if 语句的问题。我被告知功能gopostcodeChange没有定义。

我还被告知flagif 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');
}
}
4

4 回答 4

2

未定义的函数是语法错误的直接后果 - 语法错误,函数无法理解,因此未定义。

所以,关于那个语法错误,我很确定if条件句周围的括号是强制性的:

if( flag == 1)

编辑:另外,正如 Wouter 指出的那样,您错过了}结束distanceCheck函数定义的 a 。还有一件事,请终止您的台词;或为令人讨厌的惊喜做好准备。否则 JavaScript 会搞砸你的!

于 2013-06-25T10:16:57.180 回答
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
}
}

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');
}
}
于 2013-06-25T10:16:44.910 回答
0

If 语句不正确:

采用

if(flag==1){//your statement}
else {//your statement}

的功能范围distanceCheck()不正确。

于 2013-06-25T10:19:15.780 回答
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:
}
于 2013-06-25T10:20:14.313 回答