HTML Form:
<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();"
' only CheckTime works if i call ChecKBreakTime first it only works.
The Functions (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:
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...
JavaScript uses short-circuit evaluation for its logical operators. That means that in an expression like expr1 && expr2
, expr2
is evaluated only if expr1
evaluates to true. 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.
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. 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.
If you want both functions to run even if the first one evaluates to false - then you can use "&": onsubmit multiple javascript functions
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).
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.