開發(fā)MVC+jQuery下B/S系統(tǒng)的表單提交
我們這里將說到如果在ajax提交表單,但這里將介紹MVC+jQuery的處理方法。需要實現(xiàn)的是1、jquery.form.js的使用、2、lightbox的實現(xiàn)、3、如何驗證。
說起表單提交,是無人不知,無人不曉哇。今天我們就談如何用MVC+jQuery來處理表單的提交。
想達到的效果:
1、提交前的表單驗證
2、表單驗證
3、提交后的反饋信息
ok,先說一下工作的原理。
前臺 ,action指定了接受表單的處理頁面。method這里我們只說post
如果用ajax提交表單,自然xxx.aspx便是請求的url。ajax請求后的callback便是響應(yīng)表單的提交結(jié)果(錯誤、成功),我們提交的反饋信息用一個 浮層(lightbox)來表示。 不至于用 alert(""); 這樣鐺鐺鐺很囧。
我們引入一個jQuery的插件http://www.malsup.com/jquery/form/#api 這是一個略有名氣的插件,方便易用。
關(guān)于其用法,大家可以搜索下。
那么我們需要做的就是
1、jquery.form.js的使用
2、lightbox的實現(xiàn)
3、如何驗證
Code
- //注冊form的ajaxForm 此方法需要調(diào)用jquery.ajaxwindow.js的方法
- //一般form里有action,所以參數(shù)有可能只需要傳一個callback,
- //如果一個表單有多個提交按鈕,那么則需要 提交不同的url
- // 這個方法是用來注冊form提交,如果有多個提交按鈕時,最好默認注冊一個,否則驗證方法就不起到作用
- $.fn.submitForm = function(args) {
- var url, id, callback, before;
- id = this.attr("id");
- if (typeof (args) == "string") {//只傳一個url
- url = args;
- before = undefined;
- callback = undefined;
- }
- else {
- args = args || new Object();
- url = args.url || this.attr("action");
- if (typeof (args) == "function") {//只傳一個callback
- callback = args;
- }
- else {
- before = args.before;
- callback = args.callback;
- }
- }
- //輸入驗證
- this.inputValidate();//這是我們需要實現(xiàn)的一個“輸入時的驗證”,
- this.ajaxForm({ //這里調(diào)用jquery.form.js表單注冊方法。
- url: url,
- beforeSubmit: function(a, f, o) {//提交前的處理
- //提交驗證
- if ($("#" + id).submitValidate() == false)//這里我們需要實現(xiàn)的“提交時的驗證”。
- return false;
- if (before != undefined && before() == false) {
- return false;
- }
- o.dataType = "json";//指定返回的數(shù)據(jù)為json格式。
- },
- success: function(data) {//提交后反饋信息處理
- if (data == "" || data == null) {
- return false;
- }
- var msg = new ajaxMsg(data);//這個ajaxMsg便是我們需要實現(xiàn)的Lightbox
- msg.show(callback);//show這個反饋的結(jié)果。
- return false;
- }
- });
- return this;
- }
上面的方法很簡單,就是form插件的使用,還有幾個我們需要實現(xiàn)的方法。(inputValidate,submitValiedate,ajaxMsg)
既然是ajax提交,我們則需要給客戶端一個反饋信息,然后用Lightbox呈現(xiàn)。
一:我們定義一個JsonMessage類
- Code
- public class JsonMessage
- {
- public int result { get; set; } //0錯誤 1正確 2提示 3警告
- public string title { get; set; }//Lightbox窗口的標題
- public string content { get; set; }//message的具體內(nèi)容
- public string redirect { get; set; }//彈出后自動跳轉(zhuǎn)的到哪里?
- public object callBackData { get; set; }//客戶端需要的數(shù)據(jù) 比如 一個新增的id或整個model
MVC返回Json(jsonmsg1),客戶端的callback便可以得到這個對象的json格式。
(注意:如果是附件的表單提交,則不能識別JsonResult格式。具體我還沒有研究怎么回事,這種情況只能response一個json字符串,可以用System.Web.Script.Serialization.JavaScriptSerializer來序列化對象,也很方便。)
二:我們寫一個ajaxMsg來實現(xiàn)lightbox,這是我自己寫的Lightbox,比較簡陋,如果不喜歡,也可以用一些知名的插件。
- Code
- var ajaxMsg = function(msg) {
- if (msg != undefined) {
- //提示框的屬性
- this.result = msg.result || 0; //0錯誤 1正確 2提示 3警告
- this.title = msg.title || ""; //提示框的標題
- this.redirect = msg.redirect || null;
- this.content = msg.content || ""; //窗口的內(nèi)容 通過后臺json數(shù)據(jù)獲得
- this.callBackData = msg.callBackData;
- }
- }
- //創(chuàng)建一個win
- ajaxMsg.prototype.open = function(parentElement) {
- //創(chuàng)建Mask遮罩層
- $("body").append("
- ");
- //設(shè)置Mask高度
- var bodyheight = document.body.offsetHeight;
- var clientheight = document.documentElement.clientHeight;
- if (bodyheight > clientheight)
- clientheight = bodyheight;
- else
- bodyheight = clientheight;
- $("#MsgMask").height(bodyheight + "px");
- //創(chuàng)建窗口
- $("body").append("
- ");
- var frameId = "#MsgFrame";
- //不同的style 不同的frame frame由mask來控制
- switch (this.result) {
- case 0:
- $(frameId).addClass("msg_error");
- break;
- case 1:
- $(frameId).addClass("msg_right");
- break;
- case 2:
- $(frameId).addClass("msg_tips");
- break;
- case 3:
- $(frameId).addClass("msg_warning");
- break;
- default:
- $(frameId).addClass("msg_default");
- break;
- }
- //創(chuàng)建內(nèi)容div
- $(frameId).append("
- " + this.content + "
- ");
- //設(shè)置Mask的寬高
- if (this.width > 0)
- $(frameId).width(this.width);
- if (this.height > 0)
- $(frameId).height(this.height);
- //設(shè)置窗口的定位
- var frame_width = $(frameId).width();
- var frame_height = $(frameId).height();
- var css_top = parseInt((document.documentElement.clientHeight - frame_height) / 2) - 100;
- if (css_top <= 0)
- css_top = 80;
- var css_left = (document.documentElement.clientWidth - frame_width) / 2;
- var css_right = css_left;
- $(frameId).css("top", css_top + "px").css("left", css_left + "px").css("right", css_right + "px");
- hideSelects();
- }
- //顯示方式
- ajaxMsg.prototype.show = function(callBack) {
- if (this.result == undefined) {
- if (callBack != undefined)
- callBack(this.callBackData);
- return;
- }
- if (this.result == 1) {
- if (this.content != undefined && this.content != "") {
- //this.open();
- //setTimeout(this.close, 1000);
- alert(this.content);
- }
- if (callBack != undefined)
- callBack(this.callBackData);
- }
- else {
- //this.open();
- //setTimeout(this.close, 2000);
- alert(this.content);
- }
- }
- //關(guān)閉
- ajaxMsg.prototype.close = function() {
- removeMsg();
- }
- function removeMsg() {
- $("div").remove("#MsgMask");
- $("div").remove("#MsgFrame");
- showSelects();
- }
不知道有沒有人能理解我這里的callback,我說一下他的用到的情況。 實際應(yīng)用中我還有一個ajaxWin來實現(xiàn)彈窗,彈窗里的表單提交后一般需要關(guān)閉彈窗,然后更新一些數(shù)據(jù)(比如刷新列表)。這時就需要 submit后的callback動作。另外就是我目前還有使用到redirect這個屬性。呵呵,我之前的系統(tǒng)需要大量的跳轉(zhuǎn)處理,這些跳轉(zhuǎn)也是無刷新的,有一個數(shù)組來記錄訪問的地址??梢詫崿F(xiàn)后退和前進。
下面是他的css
- Code
- .mask
- {
- background-color: #fff;
- width: 100%;
- height: 100%;
- position: absolute;
- display: block;
- top: 0px;
- left: 0px;
- filter:alpha(opacity=0);-moz-opcacity:0;opacity:0;
- z-index:1;
- }
- .frame
- {
- position: absolute;
- z-index: 2;
- min-width:400px;
- font-size:12px;
- background-color:#fff;
- overflow:hidden
- }
- .ajaxwin
- {
- border: 1px solid #ccc;
- text-align:left;
- }
- .frame .head
- {
- line-height:25px;
- background-color: #d3eaff;
- clear:both;
- }
- .frame .head .title
- {
- position:relative;
- left:5px;
- font-size: 12px;
- font-weight: bold;
- color: #000;
- }
- .frame .head .btnclose
- {
- position:absolute;
- top:0px;
- right:5px;
- }
- .default
- {
- border: 1px solid #95A1B6;
- }
- .msg_error
- {
- border: 2px solid #FF6600;
- background-color: #FFFFCC;
- font-size: 14px;
- overflow: hidden;
- font-weight: bold;
- }
- .msg_error .head,.msg_tips .head,.msg_right .head{display:none;}
- .msg_tips,.msg_default
- {
- border: 2px solid #3399FF;
- background-color: #E6FFFF;
- font-size: 14px;
- height: 60px;
- overflow: hidden;
- font-weight: bold;
- }
- .msg_right
- {
- border: 2px solid #009933;
- background-color: #E8FFD0;
- font-size: 14px;
- height: 60px;
- overflow: hidden;
- font-weight: bold;
- }
- .content{ padding:25px; text-align:center;}
- .default .content,.ajaxwin .content{ padding:10px; text-align:left; }
- .frame .btnclose
- {
- padding:5px;
三:好了,Lightbox已經(jīng)實現(xiàn)了,也能show出各種類型的信息了。
下面還剩下表單驗證。 其實表單驗證大有文章可做。我這也不能一一做到。目前只做了些簡單的驗證。以后會實現(xiàn)比較復雜的錯誤提示效果。其實這都是 體力活,上面沒要求我也懶的弄-。-
驗證我采用的是給control一些自定義屬性,然后再判斷其值是否合法。
- Code
- //輸入驗證
- $.fn.inputValidate = function() {
- $("input,select,textarea", this).each(function() {
- var isnull = $(this).attr("isnull");
- var regexValue = $(this).attr("regex");
- var defautValue = $(this).attr("dvalue");
- // //①非空注冊焦點事件
- // if (isnull == "0") {
- // $(this).blur(function() {
- // if (this.value == "" || this.value == defautValue)
- // $(this).addClass("focus").focus();
- // else
- // $(this).removeClass("focus");
- // });
- // }
- //②正則注冊onchange事件
- if (regexValue != undefined) {
- var thisValue = this.value;
- //檢查類型綁定不同事件
- if ($(this).attr("type") == "text") {
- $(this).bind("keyup", function() {
- if ($(this).val() == "")
- return;
- var re = new RegExp(regexValue, "ig");
- var newValue = this.value;
- if (!re.test(newValue)) {
- $(this).val(thisValue).focus();
- }
- else {
- thisValue = newValue;
- $(this).val(newValue);
- }
- });
- }
- }
- function checkRegex(value, re) {
- }
- //③最小長度
- //④其他
- });
- }
- //提交驗證
- $.fn.submitValidate = function() {
- var result = true;
- $("input:visible,select:visible,textarea:visible", this).each(function() {
- result = true;
- var thisValue = "";
- if ($(this).attr("type") == "radio" || $(this).attr("type") == "checkbox") {
- thisValue = $("input[name='" + this.name + "']:checked").val();
- }
- else
- thisValue = $(this).val();
- //判斷是否違法
- if ($(this).attr("isnull") == "0") {//① 是否必填 且不能為空或缺省值
- result = (thisValue != "" && thisValue != $(this).attr("dvalue"));
- }
- else if (thisValue != "") {//② 是否符合格式 屬性為 regex 正則
- var reValue = $(this).attr("regex");
- if (reValue != undefined) {
- re = new RegExp(reValue, "ig");
- result = re.test(thisValue);
- }
- }
- // //③ 是否符合最大長度
- // var maxLength = $(this).attr("maxLen");
- // if (maxLength != undefined && maxLength != "-1") {
- // if (thisValue.length > parseInt(maxLength))
- // result = false;
- // }
- // //④ 是否符合最小長度
- //返回false
- if (result == false) {
- $(this).addClass("focus").focus().blur(function() {
- if (this.value != "" && this.value != $(this).attr("dvalue")) {
- $(this).removeClass("focus");
- }
- });
- //alert($(this).attr("name"));
- return false;
- }
- });
- return result;
- }
原文標題:MVC+Jquery開發(fā)B/S系統(tǒng):③表單提交
鏈接:http://www.cnblogs.com/mad/archive/2009/09/14/1566231.html
【編輯推薦】
- jQuery調(diào)用WCF服務(wù)傳遞JSON對象
- 學習jQuery必須知道的幾種常用方法
- 用XML+XSLT+CSS+JQuery組建ASP.NET網(wǎng)站
- 使用jQuery和PHP構(gòu)建一個受Ajax驅(qū)動的Web頁面
- jQuery調(diào)用WCF需要注意的一些問題
【責任編輯:彭凡 TEL:(010)68476606】