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();
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程