简体   繁体   中英

How to prevent page from redirecting if false? (HTML/JavaScript)

Using - HTML, JavaScript, JQueryMobile, Phonegap. (One page architecture, all pages are on one html page)

I have an if/else statement for user login, so the if statement directs the user to #page4 (if user/pass found in the database) which works perfectly fine, however I currently have a notification for the else statement but the issue is that after the notification it redirects the user back to the index page instead of remaining on the same page and allowing the user to try again.

What can I put in the else to prevent the page from redirecting off the current page, so that users can have another attempt at signing in?

See below my current code

function loginUser()
{
  db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2*1024*1024);
  db.transaction(loginDB, errorCB);
}

function loginDB(tx)
{
  var Username = document.getElementById("username").value;
  var Password = document.getElementById("password").value;
  tx.executeSql("SELECT * FROM SoccerEarth WHERE UserName='" + Username + "' AND Password= '" + Password + "'", [], renderList);

}
function renderList(tx,results) {
  if (results.rows.length > 0) {
    navigator.notification.alert("Login Success!");
    window.location = "#page4";
  }
  else
  {
    navigator.notification.alert("Incorrect!");
    return false;
  }
}

Event -

  <div data-role="content">

    <form>
        <label for="username">Username</label>
        <input type="text" id="username">
        <br>
        <label for="password">Password</label>
        <input type="password" id="password" >

        <input type="submit" value="Sign In" onclick="return loginUser()">

    </form>
</div>

代替onclick for button,使用<form onsubmit="return loginUser()">

尝试使用#将动作属性设置为表单。

<form action='#'> //your html code... </form>

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.

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