weixin_33716557 2015-02-03 08:39 采纳率: 0%
浏览 134

onsubmit不触发我的JS

what am i missing here?

<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>

have i got a spelling mistake? my console.logs doesnt show either

  • 写回答

3条回答 默认 最新

  • 妄徒之命 2015-02-03 08:46
    关注

    In your validateLogIn() function, you aren't returning false, therefore the function will be called, but the browser will still proceed to submit the form to crud.html and change page.

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

    If you want to submit to crud.html only after the success function fires, then you still need the above return false, and in your success function add:

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

报告相同问题?