2013-06-25 51 views
0

我遇到了兩個函數和if語句的問題。我被告知功能gopostcodeChange未定義。定義函數和if語句問題

我也被告知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'); 
} 
} 
+1

'如果標誌== 1'應該是'如果(標誌== 1)'..採用全縮進會爲你節省很多的麻煩了。 –

回答

2

被定義的功能是語法錯誤的直接後果 - 與語法錯誤時,該功能不能被理解,因此不限定。

所以,關於該語法錯誤,我敢肯定,圍繞if conditonal括號是強制性的:

if(flag == 1) 

編輯:另外,作爲沃特指出,你錯過了}結束你distanceCheck功能確定指標。另一件事,請終止您的線路;或準備討厭的驚喜。 否則JavaScript會把你搞砸!

+0

Naah。不是在這種情況下。只有當這樣的東西寫成:'var a =「hello World」,b = a',然後在下一行'(function(){/*...*/})()'這將被評估as'b = a(function(){})()' – andlrc

+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'); 
} 
} 
0

if語句是不正確的:

使用

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

distanceCheck()功能範圍不正確。

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: 
}