MVC+jQuery开发B/S系统:表单提交

开发 后端
B/S系统是基于B/S结构的系统,也就是基于浏览器/服务器的系统。本文主要介绍MVC+jQuery来处理表单的提交,让我们一起来看。

Jquery是一个优秀的Javascrīpt框架。MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。今天我们就谈如何用JQuery+MVC来处理表单的提交。 

想达到的效果:

1、提交前的表单验证 

2、表单验证 

3、提交后的反馈信息 

ok,先说一下工作的原理。

前台<form action='xxx.aspx' method='post'></form>,action指定了接受表单的处理页面。method这里我们只说post。 如果用ajax提交表单,自然xxx.aspx便是请求的url。ajax请求后的callback便是响应表单的提交结果(错误、成功),我们提交的反馈信息用一个 浮层(lightbox)来表示。 不至于用 alert(""); 这样铛铛铛很囧。

我们引入一个Jquery的插件http://www.malsup.com/jquery/form/#api 这是一个略有名气的插件,方便易用。关于其用法,大家可以搜索下。

那么我们需要做的就是: 

1、jquery.form.js的使用 

2、lightbox的实现 

3、如何验证

代码:

  1. $.fn.submitForm = function(args)   
  2. {          
  3. var url, id, callback, before;   
  4. id = this.attr("id");          
  5. if (typeof (args) == "string")   
  6. {              
  7. url = args;              
  8. before = undefined;              
  9. callback = undefined;          
  10. }          
  11. else   
  12. {              
  13. args = args || new Object();              
  14. url = args.url || this.attr("action");              
  15. if (typeof (args) == "function")   
  16. {                  
  17. callback = args;              
  18. }              
  19. else   
  20. {                  
  21. before = args.before;                  
  22. callback = args.callback;              
  23. }          
  24. }          
  25. //输入验证          
  26. this.inputValidate();          
  27. //form没有url 则是伪提交          
  28. if (url == undefined || url == "")   
  29. {              
  30. $("#" + id).submit(function() {                  
  31. if ($("#" + id).submitValidate()==false)                      
  32. return false;                  
  33. //验证成功就执行callback                  
  34. callback();              
  35. });          
  36. }          
  37. else   
  38. {              
  39. this.ajaxForm({                  
  40. url: url,                  
  41. beforeSubmit: function(a, f, o)   
  42. {                      
  43. //提交验证                      
  44. if ($("#" + id).submitValidate() == false)                          
  45. return false;                      
  46. if (before != undefined && before() == false) {                          
  47. return false;                      
  48. }                      
  49. o.dataType = "json";                  
  50. },                  
  51. success: function(data) {                      
  52. if (data == "" || data == null)   
  53. {                          
  54. return false;                      
  55. }                      
  56. $("#myMsg").showMsg(data, callback);                    
  57. return false;                  
  58. }              
  59. });          
  60. }          
  61. return this;      
  62. }; 

上面的方法很简单,就是form插件的使用,还有几个我们需要实现的方法。(inputValidate,submitValiedate,ajaxMsg)
既然是ajax提交,我们则需要给客户端一个反馈信息,然后用Lightbox呈现。

#p#

一:我们定义一个JsonMessage类

  1. public class JsonMessage  
  2. {  
  3. public int result { getset; } //0错误 1正确 2提示 3警告  
  4. public string title { getset; }//Lightbox窗口的标题  
  5. public string content { getset; }//message的具体内容  
  6. public string redirect { getset; }//弹出后自动跳转的到哪里?  
  7. public object callBackData { getset; }//客户端需要的数据 比如 一个新增的id或整个model  
  8. }  
 

MVC返回Json(jsonmsg1),客户端的callback便可以得到这个对象的json格式。
(注意:如果是附件的表单提交,则不能识别JsonResult格式。具体我还没有研究怎么回事,这种情况只能response一个json字符串,可以用System.Web.Script.Serialization.JavaScriptSerializer来序列化对象,也很方便。)

二:我们写一个ajaxMsg来实现lightbox,这是我自己写的Lightbox,比较简陋,如果不喜欢,也可以用一些知名的插件。

代码:

  1. (function($)   
  2. {   
  3. $.fn.showMsg = function(model, callback, fail)   
  4. {   
  5. var me = this;   
  6. if (me.length == 0)   
  7. {   
  8. $("body").append("<div id='" + me.selector.replace("#""") + "'></div>");   
  9. $(me.selector).showMsg(model, callback);   
  10. return;   
  11. }   
  12. if (model == undefined)   
  13. return;   
  14. model.content = model.result == 1 && model.content == undefined ? "操作成功!" : model.content;   
  15. me.html(model.content);   
  16. me.removeClass().addClass("message_" + model.result).show(100);   
  17. if (model.result1 == 1 && fail != undefined)   
  18. {   
  19. fail(model);   
  20. }   
  21. if (model.result == 1 && callback != undefined)   
  22. {   
  23. callback(model);   
  24. }   
  25. setTimeout(function()   
  26. {   
  27. if (me.css("display") != "none")   
  28. {   
  29. me.hide(100);   
  30. }   
  31. }, 3000);   
  32. return me;   
  33. }  
  34. })(jQuery);  

Ajax消息的样式完全可以用CSS来做,包括div的定位都可以在css里写js代码来实现。

不知道有没有人能理解我这里的callback,我说一下他的用到的情况。 实际应用中我还有一个ajaxWin来实现弹窗,弹窗里的表单提交后一般需要关闭弹窗,然后更新一些数据(比如刷新列表)。这时就需要 submit后的callback动作。另外就是我目前还有使用到redirect这个属性。呵呵,我之前的系统需要大量的跳转处理,这些跳转也是无刷新的,有一个数组来记录访问的地址。可以实现后退和前进。

三:好了,Lightbox已经实现了,也能show出各种类型的信息了。
下面还剩下表单验证。 其实表单验证大有文章可做。我这也不能一一做到。目前只做了些简单的验证。以后会实现比较复杂的错误提示效果。其实这都是体力活,上面没要求我也懒的弄。

验证我采用的是给control一些自定义属性,然后再判断其值是否合法。

代码:

  1. //输入验证   
  2. $.fn.inputValidate = function() { $("input,select,textarea"this).each(function() {   
  3. var me = $(this);   
  4. var isnull = me.attr("isnull") || "1";   
  5. var dvalue = me.attr("dvalue");   
  6. me.focus(function() {   
  7. if (this.value == "") $(this).inputRemove();   
  8. });   
  9. me.keyup(function() { if (this.value == "") $(this).inputRemove(); });   
  10. //①非空检查 if (isnull == "0") {   
  11. me.blur(function() {   
  12. if ($(this).val() == "" || $(this).val() == dvalue) $(this).inputError("此项必须填写!");   
  13. else $(this).inputRight();   
  14. });   
  15. }   
  16. //②正则注册onchange事件   
  17. var regexValue = me.attr("regex");   
  18. if (regexValue != undefined) {   
  19. var thisValue = me.val();   
  20. me.blur(function() {   
  21. var re = new RegExp(regexValue, "ig");   
  22. if (this.value != "" && !re.test(this.value)) { me.inputError("格式不正确!");   
  23. return result = false;   
  24. else me.inputRight();   
  25. }); }   
  26. //③最小长度   
  27. var minLength = me.attr("minlength") || 0;   
  28. if (minLength != undefined) minLength = parseInt(minLength);   
  29. me.blur(function() {   
  30. if (me.val() != null)   
  31. if (me.val().length < minLength) {   
  32. me.inputError("长度不能小于" + minLength); }   
  33. });   
  34. //④其他   
  35. });   
  36. };   
  37. //提交验证   
  38. $.fn.submitValidate = function() {   
  39. var result = true; $("input:visible,select:visible,textarea:visible"this).each(function() {   
  40. var me = $(this);   
  41. var thisValue = "";   
  42. if (me.attr("type") == "radio" || me.attr("type") == "checkbox") {   
  43. thisValue = $("input[name='" + this.name + "']:checked").val();   
  44. }   
  45. else thisValue = me.val();   
  46. //判断是否违法   
  47. //① 是否必填 且不能为空或缺省值   
  48. if (me.attr("isnull") == "0") {   
  49. if (thisValue == "" || (thisValue != "" && thisValue == me.attr("dvalue"))) {   
  50. me.inputError("此项必须填写!");   
  51. return result = false;   
  52. }   
  53. else me.inputRight();   
  54. }   
  55. //② 是否符合格式 属性为 regex 正则   
  56. if (thisValue != "") {   
  57. var reValue = $(this).attr("regex");   
  58. if (reValue != undefined) {   
  59. re = new RegExp(reValue, "ig");   
  60. if (!re.test(thisValue)) {   
  61. me.inputError("格式不正确!");   
  62. return result = false;   
  63. }   
  64. else me.inputRight();   
  65. } } });   
  66. return result;   
  67. };   
  68. $.fn.inputError = function(msg) {   
  69. this.inputRemove();   
  70. //this.focus();   
  71. $("span"this.parent()).hide();   
  72. this.parent().addClass("focus").append("<span class='error'>" + (msg || '') + "</span>");   
  73. }   
  74. $.fn.inputRight = function(msg) {   
  75. this.inputRemove();   
  76. //this.focus();   
  77. $("span"this.parent()).hide();   
  78. this.parent().addClass("focus").append("<span class='right'>" + (msg || '') + "</span>");   
  79. } $.fn.inputRemove = function() {   
  80. this.removeClass("focus");   
  81. $(".right,.error"this.parent()).remove();   
  82. $("span"this.parent()).show();   
  83. }  
 

每一种方法,我们都应该从性能和发功效率上来考虑,要做到两者兼得是很不容易的。通过本文作者的分析,希望会对你有所帮助。

责任编辑:于铁 来源: 博客园
相关推荐

2009-09-14 18:44:06

MVC+jQuery

2012-06-05 10:15:43

jQuery

2009-07-29 16:40:50

Ajax提交asp.n

2012-12-24 10:00:07

ASP.NETjQueryAjax

2009-06-25 14:32:00

Java BS开发模式

2010-11-23 16:56:04

mysql表单

2010-07-29 16:38:14

Flex表单

2013-11-13 11:01:14

表单表单重复提交表单策略

2023-04-11 07:50:27

软件架构设计

2011-06-16 10:21:52

jQuery

2013-11-13 14:39:53

表单提交开发

2010-03-04 11:36:02

Python提交表单

2009-12-01 18:11:03

PHP表单重复提交

2012-03-08 11:23:09

jQuery Mobi

2012-03-27 14:34:07

Visual Stud微软MVC

2010-01-22 14:59:40

VB.NET表单自动提

2009-05-18 10:11:06

MVCXML动态表单

2012-03-29 09:27:49

WEBjQuery

2011-06-28 09:24:44

jQuery

2009-06-30 15:19:55

Form表单JSP入门
点赞
收藏

51CTO技术栈公众号