简体   繁体   中英

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. However, it is failing to be invoked successfully and as a result the validateForm() function is terminated prematurely.

<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? If so, how?

Declaring function arguments is incorrect in 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:

function isFilled(parameter){ // checks that the given string isn't empty
    if(parameter == "" || parameter == " "){
        return false;
    }else{
        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM