2012-06-01 40 views
-1

如果您運行此腳本,並在它從來沒有運行其他的提示框中輸入2,我不知道爲什麼。這是我如何設置不正確的var或myFunction。在我的測試腳本中,我得到了我的if語句運行,但我從來沒有得到其他的運行?我做錯了

<!DOCTYPE html> 
<html> 
<body> 

<p>Click the button to demonstrate the prompt box.</p> 

<button onclick="myFunction()">Try it</button> 

<script type="text/javascript"> 
function myFunction() 
{ 
var x=1; 
var name=prompt("Please enter a number one"); 

if (x===1) 
    { 
alert("good"); 
    } 
else 
    { 
alert("not a number 1"); 
    } 

} 
</script> 

</body> 
</html> 

回答

0

因爲你定義在你的腳本x=1,然後檢查x === 1

也許你的意思是用戶輸入指定給x,但你不能這樣做。

或者正如zzzzBov所說,也許你打算根據你的目的去做x === namex == name

+0

他總是可以使用'parseInt'如果他希望用戶輸入一個數字。 – xbonez

+0

反正編輯以包括兩者。 – xbonez

4

我相信你要檢查x == name

你檢查x === 1,因爲你已經設置x = 1,從來沒有覆蓋它是總是如此。

此外,x === name將不起作用prompt返回一個字符串,1是一個數字。如果您打算使用嚴格比較,則可能需要設置x = '1',然後檢查x === name

+0

正確的,你的幫助 – user1324518

+0

@ user1324518,如果這是你要找的答案,請記得選擇下計票對號。 – zzzzBov

1

你永遠分配x到什麼,但1!然後,你檢查一下它確實是整數1

0

你的腳本改爲

if (prompt("Please enter a number one") != 1){ 
    alert('Not a number 1'); 
} else { 
    alert('Good') 
} 
相關問題