JS代码简洁之道

因为很久没有关注JS代码了,正巧看到表弟写的JS感觉挺好的,所以拿来与大家分享一下(代码量很少而且可读性强)。

1.JSP代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
   
  <body>
    <script src="${pageContext.request.contextPath}/js/jquery.js"></script>
	<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/testform.js"></script>
	
    用户信息管理 <br>
    
     <form id="testform" method="post"><br/><br/>
     	<input type="hidden" name="method" value="addUser"/>
     	姓名: <input type="text" name="studentName"/><br/><br/>
     	性别: <input type="text" name="studentSex" /><br/><br/>
     	地址: <input type="text" name="studentDizhi"/><br/><br/>
     	<input id="save" type="button" value="保存"/>
     </form>
   

  </body>
</html>

2.JS代码

$(function() {
	
	function test(){
		var options = {
			url : "/demo/testAction.do",
			type : "post",
			data : $("form#testform").serialize(),
			cache : false,
			async : true,
			success : function(data) {
					alert("操作成功");
					window.location.reload();
			}
		};
		$.ajax(options);
	}	
	
	function init() {
		$("input#save").click(test);
	}
	
	$(init);	
});
					
		

3.代码分析

1.功能代码分离:各方法各司其职,如test()仅仅干一件事,就是保存。

2.事件统一管理及初始化:这样很清晰哪些方法需要哪些操作,如init(),在这里面初始化绑定事件。

3.尽量使用jQuery所提供的功能:比如form表单的序列化传参$("form#testform").serialize()。

注:很多情况下我们并不注重代码的复用,如果稍加改进可以大大减少代码量,这也是平时开发需要注意和改进的地方。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值