[英]Javascript: How do you invoke a function which has parameters
As part of a validate onsubmit function, I wish a function isFilled(var str) to be invoked. 作为验证onsubmit函数的一部分,我希望函数isFilled(var str)被调用。 However, it is failing to be invoked successfully and as a result the validateForm() function is terminated prematurely.
但是,它无法成功调用,因此validateForm()函数过早终止。
<script type = "text/javascript">
function validateForm(){
...
alert(isFilled("bla bla bla"));
...
}
function isFilled(var str){ // checks that the given string isn't empty
if(str == "" || str == " "){
return false;
}else{
return true;
}
}
</script>
Furthermore I am not being informed by the browser's console of the error. 而且,浏览器的控制台没有通知我该错误。 Should I wrap my code in try/catch statements?
我应该将代码包装在try / catch语句中吗? If so, how?
如果是这样,怎么办?
Declaring function arguments is incorrect in javascript. 在JavaScript中声明函数参数不正确。 Corrected code:
更正的代码:
function isFilled(str){ // checks that the given string isn't empty
if(str == "" || str == " "){
return false;
}else{
return true;
}
}
You're trying to declare a variable in a function parameter. 您正在尝试在函数参数中声明变量。 Just give it a parameter name
只要给它一个参数名
str can be called whatever you want it to be: 您可以将str命名为:
function isFilled(parameter){ // checks that the given string isn't empty
if(parameter == "" || parameter == " "){
return false;
}else{
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.