Javascript functions
I have 3 javascript functions:
validateClk()
, validateAmPm()
and getClks()
And on two occurring events, they get executed as follows:
OnChange
- executes validateClk()
and validateAmPm()
OnClick
- executes getClks()
(getClks()
returns a boolean value)
All 3 functions run correctly, but the problem is, after getClks()
has finished execution and returns a boolean, the next function postClocks()
doe开发者_JAVA技巧sn't run. I'm very sure that the code for postClocks()
is correct as well. If I don't use the return statement for getClks()
then the getClks()
function doesn't work as expected.
Please help :(
<script type='text/javascript'>
function validateClk() {
....
var clks = clocks.value;
if (clks == "") {
alert('Enter time');
}
else { ... }
}
</script>
<script type='...'>
function validateAMPM() {
...
var ampm= ap.value;
if (ampm=="") {
alert('Enter am or pm');
}
}
</script>
<script type='text/...'>
function getClks() {
var clks= clock.value;
var ampm= ap.value;
if (clks==" && ampm="") {
alert('Enter time and am/pm');
return false;
}
else { ... }
return true;
}
</script>
<... onChange="validateClk(); validateAmPm();" />
<... button label="Submit" onClick="return getClks(); postClocks(); return false;" />
It's because you explicitly coded a return
in there.
return getClks();
postClocks();
return false;
That code will always just exit after that first return
statement. I suggest removing it.
have all of your custom functions return boolean
then change the onclick
event to this:
onClick="if(!getClks() || !postClocks()) return false;"
assuming you don't want to continue if invalid
You wrote
onClick="return getClks(); postClocks(); return false;"
You have to remove the first "return".
精彩评论