简体   繁体   English

两次返回似乎不起作用

[英]Two returns doesn't seem to work

I have html running javascript after form,我有 html 在表单后运行 javascript,

form name="mortgage" method="post" action=" "onsubmit="return completeFormValidation();">

And javascript code for validation,和用于验证的 javascript 代码,

mainFunction:主功能:

function completeFormValidation() {

return yearentry1();

return my_location();


} // End of completeFormValidation

1st function in main:主函数中的第一个函数:

function yearentry1(){

     var yearval = document.mortgage.mortYear.value;
     alert(yearval);

}

2nd function in main: main 中的第二个函数:

function my_location(){

  var numradio = document.mortgage.propLocation.length;
  var selected="";

  for(var i=0; i < numradio; i++){

    if(document.mortgage.propLocation[i].checked == true){
        selected += "Selected radio item " + i;

    }
  }

  if(selected == ""){

    document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
    return false;

  }

}

returning two times doesn't seem to work!返回两次似乎不起作用! when 1st function passes and returns TRUE the function quits and sends the form.当第一个函数通过并返回 TRUE 时,函数退出并发送表单。

is it possible if i can have all functions run in the main function and then return false if any of the functions inside main returned false?如果我可以让所有函数在 main 函数中运行,然后如果 main 中的任何函数返回 false,则返回 false 是否有可能?

The "return" statement ends the function, so you can't call anything after this. “return”语句结束了函数,所以你不能在这之后调用任何东西。

Wrong:错误的:

function completeFormValidation() {
    return my_yearentry();
    return my_location();
}

Correct:正确的:

function completeFormValidation() {
    return my_yearentry() && my_location();
}

But your my_yearentry function must have a boolean return value.但是你的 my_yearentry 函数必须有一个布尔返回值。

This won't work, because neither of your other functions are ready, but ideally you would want to do something like:这是行不通的,因为您的其他功能都没有准备好,但理想情况下您会想要执行以下操作:

function completeFormValidation() {
    return my_yearentry() && my_location();
}

The trouble is neither of the other functions always return something useful either.问题是其他函数都不总是返回有用的东西。

If they both always returned (and ideally returned true or false ) then this would work.如果它们总是返回(并且理想情况下返回truefalse ),那么这将起作用。

Perhaps也许

function yearentry1(){
    var yearval = document.mortgage.mortYear.value;
    return yearval;
}

and

function my_location(){
    if(selected == ""){
        document.getElementById("reserved").innerHTML = "<p> none radio selected </P>";
        return false;
    }
    return true;
}

although mixing together your validation checks and your validation reporting might also be problematic.尽管将验证检查和验证报告混合在一起也可能有问题。 This could be a good start.这可能是一个好的开始。

Once a function returns, that function call is finished.一旦函数返回,该函数调用就完成了。

function completeFormValidation() {

    my_yearentry(); // take away return statement, just call the function
                    // you have inconsistent function name below :"yearentry1()"

    return my_location();
}

Presumably, you would like to only submit the form if both functions return false.大概,您只想在两个函数都返回 false 时提交表单。 In that case, you would want the following:在这种情况下,您需要以下内容:

function completeFormValidation() {

    return my_yearentry() && my_location();    
}

http://jsfiddle.net/LgepA/1/ http://jsfiddle.net/LgepA/1/

&& is "and". &&是“和”。

In this case if the first function returns false, the 2nd won't even be executed.在这种情况下,如果第一个函数返回 false,则第二个函数甚至不会被执行。 If you want 2nd to always execute, you can use simply & :如果您希望 2nd 始终执行,您可以简单地使用&

function completeFormValidation(){

    return !!(my_yearentry() & my_location());   
}

http://jsfiddle.net/LgepA/3/ http://jsfiddle.net/LgepA/3/

Or in a more readable manner:或者以更易读的方式:

function completeFormValidation(){

    var yearResult = my_yearentry();
    var locationResult = my_location();

    return yearResult && locationResult;   
}

http://jsfiddle.net/LgepA/4/ http://jsfiddle.net/LgepA/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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