JavaScript调用函数不起作用

时间:2013-10-10 12:09:31

标签: 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>

3 个答案:

答案 0 :(得分:3)

您正在调用的函数将始终具有3个参数。也许你想检查参数是否为空?

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

答案 1 :(得分:2)

您应该检查arguments.length,而不是f.arguments.length

编辑: @TheMobileMushroom还指出,即使args是空字符串,参数长度也是3。您可以将其更改为

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

请勿使用arguments.length

答案 2 :(得分:0)

尝试将脚本标记放在表单标记之前,因此x,y和z将在之前声明。

相关问题