简体   繁体   English

JavaScript和VBScript函数调用

[英]JavaScript and VBScript function calling

HTML Form: HTML表单:

<form 
    name="CheckIn" 
    method="post" 
    action="check.asp" 
    onsubmit="return CheckBreakTime()  && CheckTime();" 
>

I call two functions whit onsubmit,the functions return true or false,but function works only if it's "first" ,for example if i say ' onsubmit="return CheckTime() && CheckBreakTime();" 我在onsubmit上调用了两个函数,这些函数返回true或false,但是该函数仅在它是“ first”时才起作用,例如,如果我说' onsubmit="return CheckTime() && CheckBreakTime();" ' only CheckTime works if i call ChecKBreakTime first it only works. '如果我先调用ChecKBreakTime,则只有CheckTime才有效。

The Functions (JavaScript) : 函数(JavaScript)

function CheckBreakTime(){
    if (document.getElementById('breakco').checked) {
        var BKTimeDif1 = '<%=BTimeDif%>';
        var var1 = 20 ;
        var sum1 = var1 - BKTimeDif1 ;
        if (BKTimeDif1 > 10 && BKTimeDif1 < 21) {
            alert("You were on a break longer than 10 minutes,You must wait " + sum1 + " minutes to pass to check out from break. ");
            return false;
        } else {
            return true;
        }
    } 

    else {
        return true;  
        }
    }

function CheckTime() {
    if (document.getElementById('breakci').checked) {
        var TimeDif2 = '<%=BTimeDiff%>';
        var TimeDif1 = '<%=TimeDif%>';
        if (TimeDif1 < 120) {
            alert("You must work at least two hours before you can go on a break.");
            return false;
        } else {
            if (TimeDif2 != 0 && TimeDif2 < 120) {
                alert("You must work at least two hours before you can go on another break.");
                return false;
            }
            else {
                return true;
            }
        }


    }
    else {
        return true;
    }
}

and the VBScript code that i put in JavaScript: 以及我在JavaScript中放入的VBScript代码:

Dim TimeDif
TimeDif=Round(Csng(DateDiff("n", (HourList.Fields.Item("checkInTime").Value), (Now()))), 2)
Dim BTimeDif
If Not IsNull(HourList.Fields.Item("breakCheckIn").Value) Then
BTimeDif = Round(Csng(DateDiff("n", (HourList.Fields.Item("breakCheckIn").Value), (Now()))), 2)
End If
If Not IsNull(HourList.Fields.Item("breakCheckOut").Value) Then    
Dim BTimeDiff
BTimeDiff = Round(Csng(DateDiff("n", (HourList.Fields.Item("breakCheckOut").Value), (Now()))), 2)
End If

VBScript code works fine,it returns what it need to and JavaScript gets it.Can some tell me what is the problem... VBScript代码可以正常工作,它返回所需的内容,JavaScript可以获取它。可以告诉我这是什么问题...

JavaScript uses short-circuit evaluation for its logical operators. JavaScript将短路评估用于其逻辑运算符。 That means that in an expression like expr1 && expr2 , expr2 is evaluated only if expr1 evaluates to true. 这意味着,在类似expr1 && expr2 ,仅当expr1计算结果为true时, expr2进行计算。 If expr1 evaluates to false there is no need to evaluate expr2 , since the result of expr1 && expr2 will be to false no matter what. 如果expr1评估为false,则无需评估expr2 ,因为无论如何expr1 && expr2的结果将为false。

So, if you run for example 因此,如果您以跑步为例

var result = foo() && bar();

function foo(){
  console.log("running foo!");
  return false;
}

function bar(){
  console.log("running bar!");
  return false;
}

Only function foo() will be executed. 仅函数foo()将被执行。 This is effectively the same as: 这实际上与以下内容相同:

var result = foo();
if (result){
  result = bar();
}

That's why only the first function in your expression gets called. 这就是为什么只调用表达式中的第一个函数的原因。 If it returns false, there's no need to run the second one. 如果返回false,则无需运行第二个。

If you want both functions to run even if the first one evaluates to false - then you can use "&": onsubmit multiple javascript functions 如果您希望两个函数都运行,即使第一个函数的计算结果为false-您也可以使用“&”: onsubmit多个javascript函数

But, personally I'd wrap them both in a function that aggregates the logic of both: 但是,就我个人而言,我将它们都包装在一个汇总两者逻辑的函数中:

function checkBothTimes(){
  if (!CheckBreakTime()){
    return false;
  }
  else if (!CheckTime()){
    return false;
  }
  return true;
}

Even though this is longer - it makes the code easier to read (to me). 即使更长,它也使代码更易于阅读(对我来说)。

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

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