简体   繁体   English

从JavaScript返回false不会停止执行HTML页面

[英]returning false from JavaScript Does not stop the HTML page from Executing

i have a list in my HTML page with the following code. 我的HTML页面中有一个包含以下代码的列表。

        <li><a href="HomePage.html">Home</a></li>
        <li><a href="#" >Register</a></li>
        <li><a href="#" onclick="loginCheck();">Contact Us</a></li>
        <li><a href="MakePayment.aspx" onclick="loginCheck();">Services</a></li>
        <li><a href="HomePage.html" onclick="logout();">Log Out</a></li>

my log in check function is as follows. 我的登录检查功能如下。

 function validate()
   {
   some validations done here.if found true change the variable log in to true.
   login = true;
   }
 function loginCheck() {

       if (!login) {
           alert("Log In to continue operations");
           return false;
       }

the page gives an alert Log In to continue operations . 该页面会发出警报,提示“ Log In to continue operations But still moves to the next page which was clicked. 但是仍然移至已单击的下一页。 How can i stop this ? 我该如何阻止呢?

You need to include the return in the onclick handler, otherwise the value isn't actually returned. 您需要在onclick处理程序中包含return ,否则实际上不会返回该值。
Do it like this and it will work: 这样做,它将起作用:

<li><a href="HomePage.html">Home</a></li>
<li><a href="#" >Register</a></li>
<li><a href="#" onclick="return loginCheck();">Contact Us</a></li>
<li><a href="MakePayment.aspx" onclick="return loginCheck();">Services</a></li>
<li><a href="HomePage.html" onclick="return logout();">Log Out</a></li>

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

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