1.要使用ajaxSubmit方法,必須要加兩個js文件,且順序是:
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/jquery-form.js"></script>
2.data的由來:
data來自於Login(servlet)中傳遞過來的值,如下。。。。。。。。。。所以data=1 or data=0
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
String pwd = req.getParameter("pwd");
Dao.connect();
boolean result = Dao.login(id,pwd);
if(result)
{
resp.getWriter().print(1);
}
else{
resp.getWriter().print(0);
}
}
3.異步提交form表單使用:
<script>
$(function(){
/*異步提交form表單*/
$('#button的id').click(function(){
$('form').ajaxSubmit(function (data){
if(data == 1)
{
window.location="目標地址";
}
else
{
alter('密碼或賬戶錯誤');
}
})
})
})
</script>
<form action="Login" method="post">
<div class="center_center" id="center_center">
賬號:<input type="text" name="id"><br>
密碼:<input type="text" name="pwd">
</div>
<div class="center_buttom"><input type="button" value="登錄" id="login">
</div>
</form>
4.使用異步提交form表單的好處:
我覺得非常簡便,輕巧,效率更高,可讀性更好。
