首页 > 程序开发 > 软件开发 > 其他 >

jQuery Ajax与后台交互封装js应用

2016-08-25

公共js pagging js var currentPage=1; var maxpagesize; var message; var url = " "; var param = " "; 加一个回调函数--修改,删除通用ajax请求 function saveData(param,url,message,addDt

//公共js pagging.js
var currentPage=1;
var maxpagesize;
var message;
var url = "";
var param = "";
//加一个回调函数--修改,删除通用ajax请求
function saveData(param,url,message,addDtaFun,tb){
    if (confirm(message)) {
        $.ajax({
            url : url,
            type : "post",
            data : param,//记住,data为传送的参数 
            dataType : "json",
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                //alert("========" + XMLHttpRequest.redayState + "|" + textStatus + "|" + errorThrown);
            },
            success : function(data) {
                if (data == 1) {
                    alert("操作成功!");                 
                    tb.html("");
                    addDtaFun();
                     
                } else {
                    alert("操作失败");
                }
                 
            }
        })
         
    }   
}
//查询所有部门数据
function addDept(deptId,url) {
    var param = "";
    $.ajax({
        url : url,
        type : "post",
        data : param,//记住,data为传送的参数 
        dataType : "json",
        beforeSend : function() {
 
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            //alert("========" + XMLHttpRequest.redayState + "|" + textStatus+ "|" + errorThrown);
 
        },
        success : function(data) {
            $.each(data, function(i, d) {
                //jquery取val()取不到就取text的值  select
                var $option = $("<option></option>");
                $option.attr("value", d.c_dep_name);
                $option.text(d.c_dep_name);
                deptId.append($option);
            })
 
        }
    })
}
//根据部门进行联动
function addPerson(person, param) {
    var url = contextPath + "/emp/empList.action";
    $.ajax({
        url : url,
        type : "post",
        data : param,//记住,data为传送的参数 
        dataType : "json",
        beforeSend : function() {
 
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
 
            alert("========" + XMLHttpRequest.redayState + "|" + textStatus
                    + "|" + errorThrown);
 
        },
        success : function(data) {
            $.each(data, function(i, d) {
 
                var $option = $("<option></option>");
                $option.text(d.c_emp_name);
                person.append($option);
 
            })
        }
    })
}
//查询安环部所有员工:
function addcheckPersons(id) {
    var url = contextPath + "/emp/empCheckPerson.action";
    $.ajax({
        url : url,
        type : "post",
        //data : param,//记住,data为传送的参数 
        dataType : "json",
        beforeSend : function() {
 
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
 
            alert("========" + XMLHttpRequest.redayState + "|" + textStatus
                    + "|" + errorThrown);
 
        },
        success : function(data) {
            //alert("data==="+data);
            $.each(data, function(i, d) {
                var $option = $("<option></option>");
                $option.text(d.c_emp_name);
                id.append($option);
 
            })
        }
    })
}
//分页
function pagingFunc(tb,addDtaFun) {     
    tb.html("");//清空内容      
    addDtaFun();
}
//跳转
function goPage() {
    var page=$("#page").val();
    //页数合法进行分页  
    if (valPagesize(page, maxpagesize) == true) {
        paging(page);
    }
}
 
//判断是否合法页数
function valPagesize(size,maxpagesize) {
    if (size >= 1 && size <= maxpagesize) {
        return true;
    } else if (size < 1) {
        alert("页不能小于1");
        $("#page").focus();
        return false;
    } else if (size > maxpagesize) {
        alert("输入的页数不能大于最大页数");
        $("#page").focus();
        return false;
    } else {
        alert("输入的页数不合法");
        $("#page").focus();
        return false;
    }
}
引用文件include.inc
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
<%
    //上下文路径
    String contextPath=request.getContextPath();
    //主题路径
    String themePath="/template/themes/default";
    pageContext.setAttribute("themePath",themePath);
    pageContext.setAttribute("contextPath",contextPath);
%>
<!-- 通用变量 -->
<link rel="stylesheet" href="${contextPath}${themePath}/css/layout.css" type="text/css">
<link rel="stylesheet" href="${contextPath}${themePath}/css/zfba.css" type="text/css">
<link rel="stylesheet" href="${contextPath}${themePath}/css/win.css" type="text/css">
<script type="text/javascript" src="${contextPath}${themePath}/js/jquery-1.4.2.js"></script>
<!-- 验证身份证 -->
<script type="text/javascript" src="${contextPath}${themePath}/js/checkidcard.js"></script>
<!-- loading  -->
<script type="text/javascript" src="${contextPath}${themePath}/js/page.js"></script>
 
<script type="text/javascript" src="${contextPath}/plugs/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${contextPath}/plugs/FusionCharts/JSClass/FusionCharts.js"></script>
<!-- 表格数据排序 -->
<script type="text/javascript" src="themes/default/js/jquery.tablesorter.js"></script>
 
 
<script>
    var themePath="<%=themePath%>";
    var contextPath="<%=contextPath%>";
</script>
 
 
添加页面jsp:punish_add.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
     
        <%@ include file="/template/main.inc"%>
     
     
    <div style="width: 100%">
    <div style="height: 10px">
     
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tbody><tr>
        <td width="18"><img src="" width="18" height="42" style="display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif"></td>
        <td class="win_box_a"><img src="" width="16" height="16" style="float: right; display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif">更新整改反馈结果:</td>
        <td width="18"><img src="" width="18" height="42" style="display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif"></td>
      </tr>
      <tr>
        <td class="win_box_b"> </td>
        <td style="background-color: #fff;">
            <form name="addForm" id="addForm">
                <table cellpadding="0" cellspacing="0" class="ldsy_tablist">
                    <tbody><tr>
                        <td>
                            姓名:
                        </td>
                        <td>
                            <input type="text" name="punish.name" id="name" class="tdInput">
                        </td>
                        <td>
                            违章地点:
                        </td>
                        <td>
                            <input type="text" class="tdInput" name="punish.place" id="place">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            单位:
                        </td>
                        <td>
                            <select name="punish.dept" id="dept_add" class="tdInput">
                                <option value="">请选择单位:</option>
                            </select>
                              
                        </td>
                        <td>
                            领导:
                        </td>
                        <td>
                            <select name="punish.leader" id="leader" class="tdInput">
                                <option value="">请选择领导:</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            处罚原因:
                        </td>
                        <td>
                            <textarea style="width: 100%; height: 60px" id="cause" name="punish.cause" class="tdInput"></textarea>
                        </td>
                        <td>
                            安全条律
                        </td>
                        <td>
                            <select name="punish.rules_id" class="tdInput" id="rules_id">
                                <option value="">请选择安全条律</option>
                                <option value="1">第一条</option>
                                <option value="2">第二条</option>
                                <option value="3">第三条</option>
                            </select>
                        </td>
                    </tr>
 
                    <tr>
                        <td>
                            惩罚:
                        </td>
                        <td>
                            <select name="select" class="tdInput" id="punish_type">
                                <option value="">
                                    请选择处罚方式
                                </option>
                                <option value="1">
                                    罚款
                                </option>
                                <option value="2">
                                    培训
                                </option>
                            </select>
                        </td>
                        <td>
                            <div id="chengfai">罚款金额:
                        </div></td>
                        <td>
                            <input type="text" class="tdInput" name="punish.punish_money" id="punish_money">
                            <input type="text" style="DISPLAY: none" id="traintime" name="punish.traintime" onfocus="WdatePicker({minDate:&#39;%y-%M-{%d+1}&#39;,readOnly:true})" class="tdInput">
                        </td>
                    </tr>    
             
                    <tr>
                        <td colspan="4" style="text-align:center">
                            <input type="button" class="btn_w70" value="保存" onclick="addSave()">
                               
                            <input type="reset" value="重置 " class="btn_w70">
                               
                        </td>
                    </tr>
                </tbody></table>
            </form>
            </td>
            <td class="win_box_c"> </td>
          </tr>
          <tr>
            <td valign="top"><img src="" width="18" height="15" style="display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif"></td>
            <td class="win_box_d"> </td>
            <td valign="top"><img src="" width="18" height="15" style="display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif"></td>
          </tr>
    </tbody></table>
     
 
     
 
<script>
    $("#punish_type").change(function(){
        if($("#punish_type").val()==1){
            alert($("#punish_type").val())
            $("#punish_money").show();
            $("#traintime").hide();
             
        }else if($("#punish_type").val()==2){
            $("#chengfai").text("培训时间:")
                    alert($("#punish_type").val())
            $("#punish_money").hide();
            $("#traintime").show();
        }
    })
     
     
</script>
 
主界面:
 
punish.jsp
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
<%
    //上下文路径
    String contextPath=request.getContextPath();
    //主题路径
    String themePath="/template/themes/default";
    pageContext.setAttribute("themePath",themePath);
    pageContext.setAttribute("contextPath",contextPath);
%>
<!-- 通用变量 -->
<link rel="stylesheet" href="${contextPath}${themePath}/css/layout.css" type="text/css">
<link rel="stylesheet" href="${contextPath}${themePath}/css/zfba.css" type="text/css">
<link rel="stylesheet" href="${contextPath}${themePath}/css/win.css" type="text/css">
<script type="text/javascript" src="${contextPath}${themePath}/js/jquery-1.4.2.js"></script>
<!-- 验证身份证 -->
<script type="text/javascript" src="${contextPath}${themePath}/js/checkidcard.js"></script>
<!-- loading  -->
<script type="text/javascript" src="${contextPath}${themePath}/js/page.js"></script>
 
<script type="text/javascript" src="${contextPath}/plugs/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${contextPath}/plugs/FusionCharts/JSClass/FusionCharts.js"></script>
<!-- 表格数据排序 -->
<script type="text/javascript" src="themes/default/js/jquery.tablesorter.js"></script>
 
 
<script>
    var themePath="<%=themePath%>";
    var contextPath="<%=contextPath%>";
</script>
 
 
 
safeIndex.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/template/include.inc"%>
<link rel="stylesheet" href="${contextPath}${themePath}/css/layout.css" type="text/css">
 
     
 
     
     
 
     
        <center>
            <!-- 窗体容器开始 -->
             
                <!-- 头部 -->
                <jsp:include page="include/safe_top.jsp">
                <div id="main">
                     
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tbody><tr>
                                <td>
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tbody><tr>
                                            <td width="10">
                                                <img src="" width="10" height="41"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif">
                                            </td>
                                            <td class="main_box01_t" id="menuInfo">
                                                整改反馈列表
                                            </td>
                                            <td width="10" align="right">
                                                <img src="" width="10" height="41" style="display: none;"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif">
                                            </td>
                                        </tr>
                                    </tbody></table>
                                </td>
                            </tr>
                            <tr>
                                <td height="505px" valign="top">
                                     
                                        <iframe height="100%" width="100%" border="0" scrolling="no" frameborder="0" src="include/left.jsp" noresize="true" name="left"  name="left">
                                        </iframe>
                                         
                                     
                                     
                                        <iframe height="505px" width="100%" border="0"  id="main" name="main"                                       
                                        frameborder="0" src="safe_check.jsp" style="overflow-y:scroll; overflow-x:hidden;table-layout: fixed;word-wrap:break-word;word-break:break-all">
                                        </iframe>
                                         
                                     
                                </td>
                            </tr>
                            <tr>
                                <td height="10">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tbody><tr>
                                            <td width="10">
                                                <img src="" width="10" height="10"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif">
 
                                            </td>
                                            <td width="10100" class="box_b02"></td>
                                            <td width="10" align="right">
                                                <img src="" width="10" height="10"><img alt="加载中..." title="图片加载中..." src=http://www.2cto.com/uploadfile/2016/0825/20160825015520584.gif">
                                            </td>
                                        </tr>
                                    </tbody></table>
                                </td>
                            </tr>
                        </tbody></table>
                     
                 
                <div style="height: 10px">
                 
                    <jsp:include page="include/footer.jsp">
                 
             
        </jsp:include></div></div></jsp:include></center>
     
 
<script>
function showMenuInfo(obj){
    alert(obj.text())
    $("#menuInfo").text(obj.text());
}
</script></div></div>

相关文章
最新文章
热点推荐