【发布时间】:2017-05-20 10:43:45
【问题描述】:
我在 boostrap 的模态窗口中有一个登录表单
<form method="post" id="loginForm" action="index.php">
<label for="email">Email:</label>
<input class="form-control" type="text" name="email" value="" id="emailLogin"/><br/>
<label for="password">Password:</label>
<input class="form-control" type="password" name="password" value="" id="passwordLogin"/><br/>
<div id="loginAlert" class="alert alert-danger" role="alert">Email or password incorrect</div> <!-- Hidden by default -->
<button type="submit" name="login" class="btn btn-primary" id="loginButton">Login</button>
<script src="checkLoginForm.js"></script></form>
我想在提交之前检查此表格(如果电子邮件和密码正确)。如果检查电子邮件和密码的函数返回 1,则说明有错误。在这种情况下不应该提交表单,它应该只是让警报可见。 如果一切都正确,它应该提交。
事情是:如果电子邮件和密码不正确,我可以阻止表单提交,但如果它们正确,我无法提交。这是来自checkLoginForm.js的代码
$(document).ready(function() {
$("#loginForm").submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'include/ajax.php?action=checkLogin',
data: {
email: $("#emailLogin").val(),
password: $("#passwordLogin").val(),
},
success: function(result) {
console.log(result);
if(result == 0) {
} else {
$("#loginAlert").css({"display": "block"});
}
}
});
});
});
当结果 == 0 时,我不知道该怎么做。如果我执行 $("loginForm").submit();,则不会提交表单(其他部分确实有效)。
感谢您的回复。
【问题讨论】:
-
你少了
#,应该是$("#loginForm").submit(); -
很抱歉,只是写这篇文章的时候打错了。
#在那里 - 不起作用。 -
@Denco 只需提交表单并检查您的 php 文件中提供的信息是否正确。如果不返回 0 或 false 到您的 JS 文件。