2018-10-30jquery实际应用

$(function(){
    var b ;
    var depName = $("#depName");
    var sort = $("#sort");
    //检验修改后的部门名称是否存在
    $("#depName").blur(function(){
        if(depName.val()==""){
             $("#msg").html("部门名称不能为空!");
                $("#ssb").attr("disabled",true);
                return false;
        }
        /* alert(depName.val()) */
        $.ajax({
            url : "${pageContext.request.contextPath}/DeptController?flag=checkDeptName&depName="+depName.val(),
            success:function(data){
                if(data=="ok"){
                    $("#msg").html("该部门已存在!");
                    $("#ssb").attr("disabled",true);
                }else{
                    $("#msg").html("该部门可用");
                    $("#ssb").attr("disabled",false);
                }
            }
        });
    });
    //部门名称非空判断
    $("#ssb").click(function(){
        if(depName.val()==""){
             $("#msg").html("部门名称不能为空!");
                $("#ssb").attr("disabled",true);
                return false;
        }
    });
    //排序非空判断
    $("#sort").blur(function(){
        if(sort.val()==""){
             $("#msg").html("请填入优先级!");
                $("#ssb").attr("disabled",true);
                return false;
        }
    });
    //加载所有的部门信息
    $.ajax({
        url : "${pageContext.request.contextPath}/DeptController?flag=deptManage_ajax",
        type:"post",
        dataType : "json",
        success:function(data){
            d = data;
            var mytable = $("#mytable");
            var dataObj = eval(data);
            $.each(dataObj,function(key,val){
                var id= val["id"];
                var depName = val["depName"];
                var depCreateTime = val["depCreateTime"];
                var d = depCreateTime["time"]+"";
                var sort = val["sort"];
                var date = new Date(parseInt(d.replace("/Date(\",\"").replace(")/\",\""), 10));
                var year = date.getFullYear();
                var month = date.getMonth + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                var hour = date.getHours();
                var minute = date.getMinutes();
                var second = date.getSeconds();
                var datastr = year + "-" + month + "-" + currentDate + " " + hour + ":" + minute + ":" + second;  
                mytable.append(""+id+""+depName+""+datastr+""+sort+" ");
            });
            
        }
    });
    //点击触发删除部门事件
    $("table").on("click","#del",function(){
        
        var a = $(this).attr("did");
        $.ajax({
            async : false,
            type:'post',
            dataType:'text',
            url : "${pageContext.request.contextPath}/DeptController?flag=checkPerson&depID="+$(this).attr("did") ,
            success:function(data){
                if(data=="ok"){
                    alert("当前无法删除该部门!");
                    
                }else{
                    $.ajax({
                        async : false,
                        type:'post',
                        dataType:'text',
                        url : "${pageContext.request.contextPath}/DeptController?flag=delDept&id="+a,
                        success:function(data){
                            if(data=="ok"){
                                alert("已删除该部门!");
                                window.location.href="${pageContext.request.contextPath}/admin/deptManage.jsp";
                            }
                        }
                    }); 
                }
            }
        }); 
    });
    //点击触发显示修改部门信息弹窗
    $("table").on("click","#update",function(){
        var b = $(this).attr("uid");
        $("span2").show(1000,function(){
            $.each(d,function(key,val){
                if(b==val["id"]){
                    $("#depName2").val(val["depName"]);
                    $("#sort2").val(val["sort"]);
                    $("#depid").val(val["id"]);
                }
                
            });
             });
    });
    //点击触发隐藏修改部门弹窗
    $("table").on("click","#bttn",function(){
        $("span2").hide(1000,function(){
               
             });
    });
    //点击触发修改部门信息
    $("table").on("click","#change",function(){
        //alert($("#depName2").val()+$("#depid").val());
        $.ajax({
            url : "${pageContext.request.contextPath}/DeptController",
            data :{
                flag : "updateDept",
                id : $("#depid").val(),
                depName : $("#depName2").val(),
                sort : $("#sort2").val()
            },
            success:function(data){
                if(data=="ok"){
                    alert("修改成功!");
                    window.location.href="${pageContext.request.contextPath}/admin/deptManage.jsp";
                }
            }
        });
    });
    //对修改后的部门名称进行验证是否重复
    $("#depName2").blur(function(){
        if($("#depName2").val()==""){
             $("#msg2").html("部门名称不能为空!");
                $("#change").attr("disabled",true);
                return false;
        }
        /* alert(depName.val()) */
        $.ajax({
            url : "${pageContext.request.contextPath}/DeptController?flag=checkDeptName&depName="+$("#depName2").val(),
            success:function(data){
                if(data=="ok"){
                    $("#msg2").html("该部门已存在!");
                    $("#change").attr("disabled",true);
                }else{
                    $("#msg2").html("该部门可用");
                    $("#change").attr("disabled",false);
                }
            }
        });
    });
    //部门名称非空判断
    $("#change").click(function(){
        if($("#depName2").val()==""){
             $("#msg2").html("部门名称不能为空!");
                $("#change").attr("disabled",true);
                return false;
        }
    });
    //排序非空判断
    $("#sort2").blur(function(){
        if($("#sort2").val()==""){
             $("#msg2").html("请填入优先级!");
                $("#change").attr("disabled",true);
                return false;
        }
    });
});

你可能感兴趣的:(2018-10-30jquery实际应用)