日期:2014-05-16  浏览次数:20379 次

使用闭包特性写的表单校验提交JS
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <script>
  
  /***
   * add by 张行 2010-12-16
   * 面向对象的JavaScript
   *
   */
   function $(id) 
   {
		return document.getElementById(id).value;
   }

  // 使用立即方法利用闭包创建私有静态成员方法
  var Book = (function () 
  {     
	  function checkIsbn(isbn, msg)  // 私有静态成员方法 校验方法
	  {
		  if (msg === "ok")
		  {
			  if ($(isbn) === "") 
			  { 
				return "isbn不能为空!";
			  } 
		  }
		 return msg;
	  }
	  function checkName(name, msg)  
	  {
		  if (msg === "ok")
		  {
			  if ($(name) !== "zxBook") 
			  {
				return "书名必须为zxBook!";
			  } 
		  }
		  return msg;
	  }
	 
	  // 使用闭包特性
	  return function () 
	  {
		// 私有属性
		var isbn,name,content,pageNum,msg="ok";
		
		/************* 特权set方法***************/
		this.setIsbn = function (strIsbn) 
		{
			msg = checkIsbn(strIsbn, msg)
			if (msg === "ok")
			{
				isbn = $(strIsbn);
			}
		};

		this.setName = function (strName) 
		{
			msg = checkName(strName, msg)
			if (msg === "ok")
			{
				name = $(strName);
			}
		};

		this.setContent = function (strContent) 
		{
			content = $(strContent) || "nullContent";
		};

		this.setPageNum = function (strPageNum) 
		{
			pageNum = $(strPageNum) || "0";
		};
		
		/************* 特权get方法***************/ 
		this.getIsbn = function () 
		{
			return isbn;
		};

		this.getName = function () 
		{
			return name;
		}
		
		this.getContent = function () 
		{
			return content;
		}

		this.getPageNum = function () 
		{
			return pageNum;
		}

		this.getMsg = function() 
		{
			return msg;
		}
	  }
  })() ;
  // 构建查询串
  Book.prototype.createQryString = function (isbn, name, content, pageNum) 
  {
	var str = "?" + isbn + "=" + this.getIsbn() + "&" + name + "=" + this.getName() + 
	"&" + content + "=" + this.getContent() + "&" + pageNum + "=" + this.getPageNum();
	return str;
  }

  var bookService = {
	"add" : function () 
	{
		var book = new Book();
		book.setIsbn('isbn');
		book.setName('name');
		book.setContent('content');
		book.setPageNum('pageNum');
    
		if (book.getMsg() != "ok")
		{
			alert(book.getMsg());
			return false;
		}
		else 
		{
			alert(book.createQryString('ISBN','NAME','CONTENT','NUM'));
			
			/**new Ajax.Request('${request.contextPath}/hehe.action' + book.createQryString('ISBN','NAME','CONTENT','NUM'), {
					  method: 'post',
					  onSuccess: function(transport) 
					  {		
						alert(transport.responseText);
					  }
			});**/
		}
	},
	"del" : function () {},
	"update" : function (){},
	"search" : function (){}
  }

   function ready()
   {
		document.getElementById('submitBtn').attachEvent('onclick', bookService.add);
   }

  </script>
 </HEAD>

 <BODY onload='ready();'>
	ISBN : <input type="text" name="isbn"/>
    书名: <input type="text" name="name"/>
	内容: <input type="text" name="content"/>
	页数: <input type="text" name="pageNum"/>
	<input type="button" name="submitBtn" value=" 提 交 ">
 </BODY>
</HTML>

?