返回 true 或 false 在 JavaScript 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4892239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:15:37  来源:igfitidea点击:

Return true or false not working in JavaScript

javascripthtmlformsreturn-value

提问by Christopher Hunt

My return values are not working, and I need them to work so I can validate the page. I have a function in a function because there will be more code written which will require that kind of setup.

我的返回值不起作用,我需要它们起作用以便验证页面。我在一个函数中有一个函数,因为会有更多的代码需要这种设置。

Here is the JavaScript code:

这是 JavaScript 代码:

var postalconfig = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;

function outer(){
    function checkpostal(postal_code){
      if (postalconfig.test(document.myform.postal_code.value)) {
        alert("VALID SSN");
        return true;
      } else {
        alert("INVALID SSN");
        return false;
      }
    }
  checkpostal();
}

And the HTML:

和 HTML:

<form name="myform" action="index.php" onSubmit="return outer();" method="post">
    Postal Code <input name="postal_code"  type="text" />
    <input name="Submit" type="submit"  value="Submit Form" >
</form>

回答by Martin Jespersen

Change checkpostal();to return checkpostal();

更改checkpostal();return checkpostal();

like this:

像这样:

var postalconfig = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;

function outer(){   

  function checkpostal(postal_code) {
    if (postalconfig.test(document.myform.postal_code.value)) {
      alert("VALID SSN");
      return true;
    } else {
      alert("INVALID SSN");
      return false;
    }
  }

  return checkpostal();

}

回答by lonesomeday

The problem here is that you are getting the return value of outer, but outerdoesn't return anything. return true(or false) only affects the current function, in this case, checkpostal.

这里的问题是您获得了 的返回值outer,但outer没有返回任何内容。 return true(或false) 仅影响当前函数,在本例中为checkpostal

You need to get outerto return the return value of checkpostal:

你需要得到outer返回的返回值checkpostal

function outer() {
    function checkpostal(postal_code) {
        if (postalconfig.test(document.myform.postal_code.value)) {
            alert("VALID SSN");
            return true;
        } else {
            alert("INVALID SSN");
            return false;
        }
    }

    return checkpostal();
}

回答by Jacob Mattison

Looks like at the end of outer()it should be

看起来outer()应该是最后

return checkpostal();

rather than just

而不仅仅是

checkpostal();

The call to checkpostal()may return correctly, but onsubmit won't get the result, since outer()isn't returning anything.

调用checkpostal()可能会正确返回,但 onsubmit 不会得到结果,因为outer()没有返回任何内容。

回答by wajiw

You'll want to return the call to checkpostal:

您需要将电话返回给 checkpostal:

function outer(){   

    function checkpostal(postal_code){
 if (postalconfig.test(document.myform.postal_code.value)) {
  alert("VALID SSN");
  return true;
 } else {
  alert("INVALID SSN");
  return false;
 }
}

return checkpostal();

}