2013-10-10 107 views
0

這是我的一段代碼,它不能以預期的方式工作。能不能給我任何想法,我有什麼錯JavaScript調用函數不起作用

<html> 
<body> 
<form> 
    <input type="submit" value="Check" onclick="f(x, y, z)"/> 
</form> 
</body> 
<script type="text/javascript"> 

var x = prompt("Enter the number",""); 
var y = prompt("Enter the number",""); 
var z = prompt("Enter the number",""); 

function f(x, y, z) 
{ 
    // first, check that the right # of arguments were passed. 
    if (f.arguments.length != 3) { 
     alert("function f called with " + f.arguments.length + 
       "arguments, but it expects 3 arguments."); 
     return null; 
    } 


} 
</script> 
</html> 
+0

未來,你應該說出問題所在。你期望什麼,發生了什麼。 –

+0

嗨,首先,我檢查傳遞的參數是否匹配3個參數。但是,它仍然不工作..任何想法? – Sakthivel

回答

2

您應該檢查arguments.length,不f.arguments.length

編輯: @TheMobileMushroom也指出,參數長度將是3連如果參數是空字符串。你可以把它改成

if (!x || !y || !z) 

不要使用arguments.length

+0

嗨,夥計,我仍然面臨同樣的問題。任何想法? – Sakthivel

+0

問題是什麼?該函數是否被調用? x,y和z的值是什麼? –

+0

我通過「提示」獲取用戶的價值。請參閱我的代碼。對於函數f(),我從用戶獲取值。 – Sakthivel

3

您呼叫將始終有3個參數的函數。也許你想檢查參數是否不是空的?

if (x=='' || y=='' || z==''){} 
0

嘗試將腳本標籤放置在表單標籤之前,因此x,y和z將在之前聲明。