Jquery.Form 異步提交表單的簡單實例
http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#
1. 在你的頁面里寫一個表單。一個普通的表單,不需要任何特殊的標(biāo)記:
<form id="myForm" method="post" action="/Home/AjaxForm">
<div>
Name:<input id="username" name="username" type="text" />
Password:<input id="password" name="password" type="text" />
<br />
<input type="submit" value="submit async" id="lnkSubmit" />
</div>
</form>
在沒有Jquery.Form組件的時候,提交表單,頁面會進(jìn)入阻塞模式,等待服務(wù)器端的響應(yīng)。
2. 引入jQuery和Form Plugin Javascript腳本文件并且添加幾句簡單的代碼讓頁面在DOM加載完成后初始化表單:
<head>
<script type="text/javascript" src="path/to/jquery.js"></script>
<script type="text/javascript" src="path/to/form.js"></script>
<script type="text/javascript">
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
// 為myform綁定ajaxForm異步提交事件,并提供一個簡單的回調(diào)函數(shù)。
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
</head>
加上jquery.form組件后,提交表單時,頁面不會再同步提交,而是由js做異步提交,因此提交后頁面不會有刷新。
3. 加入能夠與服務(wù)器端進(jìn)行交互的回調(diào)函數(shù)。
$(document).ready(function () {
//options是一個ajaxForm的配置對象。?
var options = {
//target: '#output1', // target element(s) to be updated with server response
//beforeSubmit: showRequest, // pre-submit callback
<FONT color=#ff0000> success: callBackFunc // post-submit callback</FONT>
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// bind form using 'ajaxForm'
$('#myForm').ajaxForm(options);
});
// responseText是服務(wù)端的響應(yīng)值。statusText是頁面
// 提交狀態(tài)值,success表示成功。
function callBackFunc(responseText, statusText) {
if (statusText == 'success') {
alert(responseText);
}
else{
alert(“服務(wù)端錯誤!”);
}
}
如果返回的是json數(shù)據(jù)則回調(diào)函數(shù)可以這么寫
function resultFunction(responseText,statusText) {
if (statusText == 'success') {
if (responseText.code == 1) {
alert(responseText.message);
}
else {
alert('error occurs!');
}
}
else {
alert('服務(wù)器錯誤!');
}
}
服務(wù)端的代碼如下:
[HttpPost]
public ActionResult AjaxForm(FormCollection form)
{
string message = "Name:" + form["username"] + " PWD: "+form["password"] ;
//return Content(message);
return Json(new { code = 1, message = message });
}
4. 加入提交前的數(shù)據(jù)校驗函數(shù)
為options對象添加 beforeSubmit屬性
var options = {
//target: '#output1', // target element(s) to be updated with server response
<FONT color=#ff0000>beforeSubmit: checkData, // pre-submit callback
</FONT> success: callBackFunc // post-submit callback
// other available options:
//url: url // override for form's 'action' attribute
//type: type // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// pre-submit callback
function checkData(formData, jqForm, options) {
// formData is an array; here we use $.param to convert it to a string to display it
// but the form plugin does this for you automatically when it submits the data
//var queryString = $.param(formData);
// jqForm is a jQuery object encapsulating the form element. To access the
// DOM element for the form do this:
var formElement = jqForm[0];
//alert('About to submit: \n\n' + queryString);
// here we could return false to prevent the form from being submitted;
// returning anything other than false will allow the form submit to continue
//return true;
if ($(formElement).find("#username").val() == "") {
alert("please enter username!");
return false;
} else {
return true;
}
}
驗證用戶名是否為空,是則提示輸入,并取消表單提交。
- jquery動態(tài)改變form屬性提交表單
- jQuery動態(tài)設(shè)置form表單的enctype值(實現(xiàn)代碼)
- jquery實現(xiàn)ajax提交form表單的方法總結(jié)
- jQuery實現(xiàn)form表單基于ajax無刷新提交方法詳解
- jquery的ajax提交form表單的兩種方法小結(jié)(推薦)
- Jquery基于Ajax方法自定義無刷新提交表單Form實例
- jQuery實現(xiàn)數(shù)秒后自動提交form的方法
- jquery中validate與form插件提交的方式小結(jié)
- jQuery改變form表單的action,并進(jìn)行提交的實現(xiàn)代碼
- jQuery ajax提交Form表單實例(附demo源碼)
- jquery實現(xiàn)動態(tài)創(chuàng)建form并提交的方法示例
相關(guān)文章
Jquery多選下拉列表插件jquery multiselect功能介紹及使用
支持點擊label實現(xiàn)checkbox組選擇,頭部選項,如全選/ 取消全選 /關(guān)閉功能,支持鍵盤選擇等等,下面與大家分享下具體使用2013-05-05
JQuery中關(guān)于jquery.js與jquery.min.js的比較探討
jquery-1.4.2.min.js是優(yōu)化的,而query-1.4.2.js是易于開發(fā)著閱讀的,具體詳解祥看本文,希望對你有所幫助2013-05-05
JQuery操作表格(隔行著色,高亮顯示,篩選數(shù)據(jù))
最近項目里對表格的操作比較多。以往我們要做一些效果的時候往往通過程序代碼來實現(xiàn),這個努力不值,因為JQuery是完全可以做到的,并且是客戶端運行,不經(jīng)過服務(wù)器處理,給用戶的反應(yīng)快,也減少了服務(wù)器壓力2012-02-02

