gpt4 book ai didi

javascript - 防止默认在 ajax 表单提交中不起作用

转载 作者:行者123 更新时间:2023-12-03 03:43:42 27 4
gpt4 key购买 nike

我尽了最大努力来实现这个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>

最佳答案

你应该使用

 <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(){


});

你的html中有两个id

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

关于javascript - 防止默认在 ajax 表单提交中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com