- 方案一
被提交的表單
<form id="loginForm" method="post">
<table align="center">
<tr>
<th align="right">用戶名</th>
<td>
<input type="text" name="username"/>
</td>
</tr>
<tr>
<th align="right">密碼</th>
<td>
<input type="password" name="password"/>
</td>
</tr>
</table>
</form>
設置一個需要提交的表單
$("#loginForm").form({
url: "<%=homePage%>/testController/login.ajax?type=form",
success: function (data) {
console.log(data);
data = eval("("+data+")");//JSON字符串轉對象
console.log(data);
if ("0" == data.st){
console.log("成功");
}else{
console.log("失敗");
}
}
});
在需要執行提交動作的地方
$("#loginForm").submit();
后台按照接收表單參數的方式進行接收
data =JSON.parseJSON(data);//JSON字符串轉對象
這種方式可以替代evalJSON.parseJSON是jQuery的方法,eval是JavaScript的方法
- 方案二
方案一是先在外部設置好表單提交的信息,然后在合適的地方執行提交動作
方案二是設置與提交在同一個地方,這里就不做演示了
除了可以設置success的回調方法外,還可以設置onSubmit的回調方法,用於執行一些提交前的前置校驗等操作,如果返回false,就會停止提交
這幾個案例都比較簡單,就不累述了
