2017-08-04 110 views
0

我在實現這個ajax的過程中嘗試了我的最佳水平。我不是什麼窗體的默認行爲發生。我給予防止默認,但它不工作。我的頁面表單沒有提交Ajax。當我嘗試點擊,而不是提交驗證碼驗證不工作。可以有人告訴這是什麼錯誤。提前致謝。防止默認不在ajax表單中工作提交

$(document).ready(function() { 
 
    $("#login").submit(function(e) { 
 
    e.preventDefault(); 
 
    $.ajax({ 
 
     type: "POST", 
 
     url: "login.php", 
 
     data: $('#login').serialize(), 
 
     success: function(data) { 
 
     alert(data); 
 
     if (data == 'success') { 
 
      location.reload(); 
 
     } else { 
 
      $('#loginmsg').html(data).fadeIn('slow'); 
 
     } 
 
     } 
 

 
    }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<form action="<?php echo $root ?>login.php" id="login" method="post"> 
 
    <div id="loginmsg"></div> 
 
    <div class="form-group"> 
 
    <label for="login_email" class="sr-only">Email address</label> 
 
    <input name="email" type="email" class="form-control" id="login_email" placeholder="Email address"> 
 
    </div> 
 
    <div class="form-group"> 
 
    <label for="login_password" class="sr-only">Password</label> 
 
    <input name="password" type="password" class="form-control" id="login_password" placeholder="Password"> 
 
    </div> 
 
    <div class="form-group"> 
 
    <img src="common/captcha/captcha.php" id="captcha" height="50" /> 
 
    <a href="javascript:void(0)" onclick="document.getElementById('captcha').src='common/captcha/captcha.php?'+Math.random();document.getElementById('captcha-form').focus()" id=change-image title="Click here to refresh captcha code"><i class="fa fa-refresh refresh-sec"></i></a> 
 
    <div class="form-item inline"> 
 
     <div class="captcha-block"> 
 
     <p class="comment-form-captcha"><input name="captcha" type="text" id="captcha-form" autocomplete="off" class="form-text contact-captcha" maxlength="6" required="required" /></p> 
 
     </div> 
 
    </div> 
 
    </div> 
 
    <div class="text-center"> 
 
    <button type="submit" class="theme_button color1"> 
 
      Log in 
 
      </button> 
 
    </div> 
 
</form>

+0

看一看這樣的:https://stackoverflow.com/questions/6462143/prevent-default-在form-submit-jquery –

+0

它不工作在我的情況下:( –

+0

@ freedomn -m爲什麼當'.submit()'應該工作得很好時,改變整個腳本的行爲? @Sarath:嘗試在你的'e.preventDefault();'之後加上'e.stopPropagation();'。 –

回答

1

你應該使用

<button type="button" class="theme_button color1">Log in</button> 

代替

<button type="submit" class="theme_button color1">Log in</button> 

和改變形式提交給

 $(document).on("click",".theme_button ", function(){ 


     }); 

你有兩個ID在你的HTML

<a class="topline-button" id="login" data-target="#" href="./" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false"> 
    <i class="fa fa-lock" aria-hidden="true"></i> Login 
</a> 


<form role="form" action="login.php" id="login" method="post"> 

</form> 
+0

無類型提交也我的表單正在提交 –

+0

答案更新。 – vel

+0

我已經嘗試點擊甚至。在這種情況下,我的驗證碼不起作用。 –

0

你可以試試這個

$(document).ready(function() { 
    $(document).on('click','#login' ,function(e) { 
    e.preventDefault(); 
    $.ajax({ 
     type: "POST", 
     url: "login.php", 
     data: $('#login').serialize(), 
     success: function(data) { 
     alert(data); 
     if (data == 'success') { 
      location.reload(); 
     } else { 
      $('#loginmsg').html(data).fadeIn('slow'); 
     } 
     } 

    }); 
    }); 
}); 
+0

我已經嘗試點擊甚至。在這種情況下,我的驗證碼不起作用 –