ajax請求後台,返回json格式資料,模板!

來源:互聯網
上載者:User

標籤:style   java   color   strong   os   資料   

添加一個使用者的時候,需要找出公司下所有的部門,和相應部門下的角色,利用ajax請求,實現聯動技術。將返回的json格式資料,添加到select標籤下。

 

<script type="text/javascript">
        //載入出部門的資訊    
        function loadGroup(){
            
                $.ajax({
                    type:"post",
                    url:"<%=request.getContextPath() %>/master/sysUser_findGroup.action",
                    async:false,
                    dataType:"json",
                    success:function(msg){
                        var group=msg["rows"];
                        if(group.length>0){
                              //擷取select標籤對象
                              var selectObj=document.getElementById("sysGroup");
                                      //將返回的部門資訊的json資料對象便利,添加到select標籤下
                                      for(var i=0;i<group.length;i++){
                                          selectObj.add(new Option(group[i].sysGroupName,group[i].sysGroupId));
                                      }
                                  }
                       }
                });
            }    
        
        
        //如果部門資訊變動,載入相應部門下的角色資訊
        function loadRole(){
            //擷取部門id
            var sysGroupId=document.getElementById("sysGroup").value;
            //當部門資訊有變化,則將原先的部門的角色清空
            var sysRole=document.getElementById("sysRole");
            sysRole.length=1;
            //當部門資訊不是‘請選擇’才發送ajax請求
            if(sysGroupId!="0"){
                $.ajax({
                    type:"post",
                    url:"<%=request.getContextPath() %>/master/sysUser_queryRole.action",
                    data:"sysGroupId="+sysGroupId,
                    async:false,
                    dataType:"json",
                    success:function(msg){
                        var role=msg["rows"];
                        if(role.length>0){
                              //擷取select標籤對象
                              var selectObj=document.getElementById("sysRole");
                                      //將返回的部門資訊的json資料對象便利,添加到select標籤下
                                      for(var i=0;i<role.length;i++){
                                          selectObj.add(new Option(role[i].sysRoleName,role[i].sysRoleId));
                                      }
                                  }
                       }
                });
            }
            loadSelectStyle();
        }
        //頁面載入完成時,載入此方法,發送ajax請求找到部門資訊
        $(document).ready(function(){
            loadGroup();
       
        });
        </script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.