Jquery.Form 異步提交表單的簡(jiǎn)單實(shí)例
http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#
1. 在你的頁(yè)面里寫(xiě)一個(gè)表單。一個(gè)普通的表單,不需要任何特殊的標(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>
在沒(méi)有Jquery.Form組件的時(shí)候,提交表單,頁(yè)面會(huì)進(jìn)入阻塞模式,等待服務(wù)器端的響應(yīng)。
2. 引入jQuery和Form Plugin Javascript腳本文件并且添加幾句簡(jiǎn)單的代碼讓頁(yè)面在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異步提交事件,并提供一個(gè)簡(jiǎn)單的回調(diào)函數(shù)。
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>
</head>
加上jquery.form組件后,提交表單時(shí),頁(yè)面不會(huì)再同步提交,而是由js做異步提交,因此提交后頁(yè)面不會(huì)有刷新。
3. 加入能夠與服務(wù)器端進(jìn)行交互的回調(diào)函數(shù)。
$(document).ready(function () {
//options是一個(gè)ajaxForm的配置對(duì)象。?
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是頁(yè)面
// 提交狀態(tài)值,success表示成功。
function callBackFunc(responseText, statusText) {
if (statusText == 'success') {
alert(responseText);
}
else{
alert(“服務(wù)端錯(cuò)誤!”);
}
}
如果返回的是json數(shù)據(jù)則回調(diào)函數(shù)可以這么寫(xiě)
function resultFunction(responseText,statusText) {
if (statusText == 'success') {
if (responseText.code == 1) {
alert(responseText.message);
}
else {
alert('error occurs!');
}
}
else {
alert('服務(wù)器錯(cuò)誤!');
}
}
服務(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ù)校驗(yàn)函數(shù)
為options對(duì)象添加 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;
}
}
驗(yàn)證用戶名是否為空,是則提示輸入,并取消表單提交。
- jquery動(dòng)態(tài)改變form屬性提交表單
- jQuery動(dòng)態(tài)設(shè)置form表單的enctype值(實(shí)現(xiàn)代碼)
- jquery實(shí)現(xiàn)ajax提交form表單的方法總結(jié)
- jQuery實(shí)現(xiàn)form表單基于ajax無(wú)刷新提交方法詳解
- jquery的ajax提交form表單的兩種方法小結(jié)(推薦)
- Jquery基于Ajax方法自定義無(wú)刷新提交表單Form實(shí)例
- jQuery實(shí)現(xiàn)數(shù)秒后自動(dòng)提交form的方法
- jquery中validate與form插件提交的方式小結(jié)
- jQuery改變form表單的action,并進(jìn)行提交的實(shí)現(xiàn)代碼
- jQuery ajax提交Form表單實(shí)例(附demo源碼)
- jquery實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建form并提交的方法示例
相關(guān)文章
jQuery實(shí)現(xiàn)文本顯示一段時(shí)間后隱藏的方法分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)文本顯示一段時(shí)間后隱藏的方法,結(jié)合實(shí)例形式分析了jQuery事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-06-06jquery 簡(jiǎn)短幾句代碼實(shí)現(xiàn)給元素動(dòng)態(tài)添加及獲取提示信息
雖然是很基本的東西,但為什么很基本的方法就可以實(shí)現(xiàn)的東西有些人偏偏還要去追求復(fù)雜高深難懂的呢?這里只是交流而已2011-09-09深入理解jQuery中l(wèi)ive與bind方法的區(qū)別
本篇文章主要是對(duì)jQuery中l(wèi)ive與bind方法的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12JQuery的read函數(shù)與js的onload不同方式實(shí)現(xiàn)
JQuery的read函數(shù)與js的onload,想必大家對(duì)這兩個(gè)方法都有所熟悉吧,接下來(lái)介紹一個(gè)實(shí)例用以上兩種方法各自實(shí)現(xiàn),感興趣的你可不要錯(cuò)過(guò)了哈,希望可以幫助到你2013-03-03基于jquery css3實(shí)現(xiàn)點(diǎn)擊動(dòng)畫(huà)彈出表單源碼特效
這篇文章主要介紹了基于jquery css3實(shí)現(xiàn)點(diǎn)擊動(dòng)畫(huà)彈出表單,彈出的表單沒(méi)有任何遮蓋層,在web前端程序開(kāi)發(fā)中經(jīng)常會(huì)用到,需要的朋友可以參考下2015-08-08jQuery下擴(kuò)展插件和拓展函數(shù)的寫(xiě)法(匿名函數(shù)使用的典型例子)
這些年,javascript火起來(lái)了,主要?dú)w功于AJAX的推廣應(yīng)用,Web2.0的發(fā)展。。。于是,出現(xiàn)了很多的javascript框架。2010-10-10完美兼容各大瀏覽器的jQuery插件實(shí)現(xiàn)圖片切換特效
網(wǎng)友zoeDylan寫(xiě)的原創(chuàng)jquery插件,實(shí)現(xiàn)了圖片輪播功能,jquery插件名稱為zoeDylan.ImgChange,圖片路徑、跳轉(zhuǎn)鏈接、提示標(biāo)題都是有動(dòng)態(tài)數(shù)組來(lái)配置,jquery插件可靈活配置6個(gè)參數(shù), height高、width寬、mgs圖片地址、links點(diǎn)擊地址、tips圖片說(shuō)明、timers自動(dòng)切換時(shí)間,2014-12-12checkbox全選/取消全選以及checkbox遍歷jQuery實(shí)現(xiàn)代碼
checkbox全選/取消全選以及checkbox遍歷jQuery實(shí)現(xiàn)代碼2009-12-12