我正在尝试通过我们的验证插件使用jquery验证表单。提交表单时,无需验证即可将其重定向到操作页面。
我也尝试过将isValidFormLogin函数也放在Index.jsp中。
Index.jsp
$filteredobject = array_filter((array)$object->maten0);
contollerMain.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap & JavaScripts-->
<script src="js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="Controllers/controllerMain.js" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap & JavaScripts-->
<title>JQuery</title>
</head>
<body>
<div class="container">
<div class="card mx-auto" style="width: 30rem;">
<div class="card-body">
<h5 class="card-title"></h5>
<form id="formLogin" action="Items.jsp" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="email" class="form-control" name="username" id="username" aria-describedby="emailHelp" placeholder="Enter Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Password">
</div>
<button name="btnLogin" id="btnLogin" type="submit" class="btn btn-primary">Login</button>
</form>
</div>
<div class="card-footer">
<a href="#">Forgot Password ?</a>
</div>
<div id="msgLogin">
<% //out.println(MsgLogin); %>
</div>
</div>
</div>
</body>
答案 0 :(得分:1)
您需要先使用event object取消活动提交。
$(document).on("click","#btnLogin",function(event){
// cancel submission
event.preventDefault();
var result = isValidFormLogin();
if(result=="true"){
$("#formLogin").submit();
}
else{
$("#MsgLogin").html(result);
}
});
答案 1 :(得分:1)
添加prevent
默认值以停止默认submit
按钮功能:
$(document).on("click","#btnLogin",function(e){
e.preventDefault();
var result = isValidFormLogin();
if(result=="true"){
$("#formLogin").submit();
}
else{
$("#MsgLogin").html(result);
}
});
答案 2 :(得分:1)
“提交”按钮具有默认的表单提交功能。您需要先提交表单,然后再进行阻止。
$(document).on("click","#btnLogin",function(e){
e.preventDefault();
//your code
});