第一个js类,包含两个方法 reset validate

<script type="text/javascript">
	var Form = Class.create();
	Form.prototype = {
		initialize: function(formId){
			this.form = $(formId);
		},
		
		reset: function(){
			for(i = 0; i < this.form.length; i++){
				if(this.form[i].type == 'text' || this.form[i].type == 'password'){
					this.form[i].value = '';
				}
			}
		},
		
		validate: function(){
			for(i = 0; i < this.form.length; i++){
				//alert(this.form[i].className);
				if(this.form[i].className.indexOf('required') > -1){
					alert(this.form[i].name + ' is required.');
				}
			}
		}
	};
	
	function clearForm(){
		var myform = new Form('loginForm');
		//myform.reset();
		myform.validate();
	}
</script>
</head>
<body>
<form id="loginForm" action="">
	<div>用户名:<input type="input" class="required" name="username" /></div>
	<div>密码:<input type="password" name="password" /></div>
	<div><input type="button" value="test" onclick="clearForm();"/> </div>
</form>

你可能感兴趣的:(prototype)