onsubmit没有解雇我的JS

时间:2015-02-03 08:39:15

标签: javascript ajax

我在这里缺少什么?

<script>       
  function validateLogIn(login)
  {
    console.log(login);
    var username  = $("#username").val();
    var password  = $("#password").val();
    console.log(username, password, login);
    $.ajax({     
      url: 'login.php',    //checking the login in                     
     data: {username:username,password:password},
     type: "POST",     //Method by which data being transmitted
     dataType: 'json',                  
     success: function(data)          
    {
       console.log(data);
       login.submit();
    } 
      //else do an alert("please lgo in again");       
  });  
  return false;
 }
 </script>
</head>
<body>
     <form action="crud.html" method="post" name="form-login" onsubmit="return validateLogIn(this);">
     <input required placeholder="Username" type="text" name="username" id="username"/>
     <input required placeholder="Password" type="password" name="password" id="password"/>
     <label for="remember">Remember Me:</label>
     <input type="checkbox" name="remember" value="yes">
     <br />
     <br />
     <input type="submit" name="login" value="login" />
 </form>
 </body>

我有拼写错误吗?我的console.logs不显示

3 个答案:

答案 0 :(得分:2)

validateLogIn()函数中,您没有返回false,因此将调用该函数,但浏览器仍会继续将表单提交到crud.html并更改页面。

function validateLogIn(login)
{
  ...
  ...
  ...
  return false;
}

如果您只想在成功功能触发后提交到crud.html,那么您仍然需要上面的return false,并在您的成功功能中添加:

success: function(data)          
{
  login.submit();
} 

答案 1 :(得分:1)

此代码在我的机器上运行正常,请确保正确包含jquery库。显然return false

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>    

      function validateLogIn()
      {
        var username  = $("#username").val();
        var password  = $("#password").val();
        console.log(username, password, this);
        alert("Hello");
        $.ajax({     
          url: 'login.php',    //checking the login in                     
         data: {username:username,password:password},
         type: "POST",     //Method by which data being transmitted
         dataType: 'json',                  
         success: function(data)          
        {
           console.log(data);
        } 
          //else do an alert("please lgo in again");       
      });  
return false;
     }
     </script>
    </head>
    <body>
         <form action="crud.html" method="post" name="form-login" onsubmit="return validateLogIn();">
         <input required placeholder="Username" type="text" name="username" id="username"/>
         <input required placeholder="Password" type="password" name="password" id="password"/>
         <label for="remember">Remember Me:</label>
         <input type="checkbox" name="remember" value="yes">
         <br />
         <br />
         <input type="submit" name="login" value="login" />
     </form>
     </body>
     </html>

答案 2 :(得分:0)

将log语句更改为如下所示,log()方法接受一个参数,因此如果要打印三个变量,请将其附加到一个字符串

console.log("username:"+username+" password:"+password+" id:"+login.id);