简体   繁体   English

Javascript:如何调用具有参数的函数

[英]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.

相关问题 您如何知道要为javascript函数设置的参数? - How do you know which parameters to set for a javascript function? 如何在html文件中声明的javascript中调用函数 - How to invoke a function in javascript which has been declared in a html file 如何在JavaScript保证字符串中使用.then函数中的参数调用函数? - How do you call a function with parameters in a .then function in a JavaScript promise string? 你如何从apache velocity * .vm文件中调用javascript函数? - How do you invoke javascript function from an apache velocity *.vm file? 如何使用JavaScript调用此函数? - How do I invoke this function with JavaScript? 如何在JavaScript中使用不同的参数再次调用生成器函数? - How to invoke a generator function a second time with different parameters in JavaScript? 如何通过JQuery或Javascript以编程方式调用服务器单击 - How do you programmatically Invoke a Server Click via JQuery or Javascript 从JavaScript中的对象调用具有正确参数的函数 - Invoke a Function with the Correct Parameters from an Object in JavaScript 如何为具有多个参数的画布函数创建循环? - How do you make a loop for canvas function which has several arguments? 您如何将传递给函数的任意数量的参数应用于JavaScript中另一个函数的参数? - How do you apply an arbitrary numbers of arguments passed to function which is argument of another function in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM