zoukankan      html  css  js  c++  java
  • 获取页面的checkbox,并给参数赋值

    需求:

    需要发送的请求:

    选中,发送name值,参数为true,不选中,发送name值,参数为false

    方法如下:

    $('#courseware_permissions_save_button').click(function(){
            //
            var boxes = $('.courseware_permission_checkbox');
            var myData = {};
            for(var i=0,j=boxes.length;i<j;i++) {
                var box = boxes[i];
                if(box.checked)
                    myData[box.value] = 'true';
                else
                    myData[box.value] = 'false';
            }
            
            jQuery.ajax( {
                url : "/direct/site/${param.siteId}/setPerms",
                type : 'POST',
                data : myData,
                async : false,
                dataType: 'text',
                success : function(data) {
                    if(data) window.location.href='${pageContext.request.contextPath}/cw/index.do';
                },
                error : function(xmlHttpRequest,status,error) {
                    alert("ERROR");               
                }
            });
           //return false;
        });

    整个页面jsp如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML>
    <html>
      <head>
          <script type="text/javascript" src="/library/js/jquery/jquery-1.9.1.min.js"></script>
      </head>
     
      <body>
      <p class="instruction">课程管理权限管理</p>
      <form action="" id="courseware_permissions_table" >
    <table class="listHier lines tableList">
        <thead>
            <tr class="listHier">
                <th align="left">角色</th>
                <th style="text-align:center;">增加</th>
                <th style="text-align:center;">删除</th>
                <th style="text-align:center;">更新</th>
                <th style="text-align:center;">数据统计</th>
            </tr>
        </thead>
    <tbody>
    </tbody>
    </table>
    </form>
    <div class="act">
        <input id="courseware_permissions_save_button" type="submit" class="active" value="保存">
        <input type="button" value="取消" onclick="javascript:window.location.href='${pageContext.request.contextPath}/cw/index.do'">
    </div>
      </body>
    <script type="text/javascript">
    $(function(){
        var perms = [];
        jQuery.ajax( {
            url : "/direct/site/${param.siteId}/perms/courseware.json",
            dataType : "json",
            async : false,
            success : function(p) {
                for(role in p.data) {
                    var permSet = {'role':role};
                    
                    $('#courseware_permissions_table').append('<tr><td align="left">'+role+'</td>'
                        +'<td align="center"><input type="checkbox" class="courseware_permission_checkbox" value="'+role.replace(/^"|"$/g,'')+':courseware.add" id="'+role.replace(/^"|"$/g,'')+'_courseware_add"></td>'
                        +'<td align="center"><input type="checkbox" class="courseware_permission_checkbox" value="'+role.replace(/^"|"$/g,'')+':courseware.delete" id="'+role.replace(/^"|"$/g,'')+'_courseware_delete"></td>'
                        +'<td align="center"><input type="checkbox" class="courseware_permission_checkbox" value="'+role.replace(/^"|"$/g,'')+':courseware.update" id="'+role.replace(/^"|"$/g,'')+'_courseware_update"></td>'
                        +'<td align="center"><input type="checkbox" class="courseware_permission_checkbox" value="'+role.replace(/^"|"$/g,'')+':courseware.diagram" id="'+role.replace(/^"|"$/g,'')+'_courseware_diagram"></td>'
                        +'</tr>');
                        
                    for(var i = 0 ; i <p.data[role].length;i++){
                        var perm = p.data[role][i].replace(/./g,"_");
                        $('#'+role.replace(/^"|"$/g,'')+'_'+perm).attr('checked',true);
                        
                    }
                    
                    perms.push(permSet);
                }
            },
            error : function(xmlHttpRequest,status,error) {
               alert("ERROR");
            }
        });
        /* $('#bbb_permissions_save_button').bind('click', function() {
                   BBBUtils.setSitePermissions('.bbb_permission_checkbox', function() {
                       // success callback
                       bbbUserPerms = new BBBPermissions(bbbCurrentUser.permissions);
                       if (bbbUserPerms.bbbViewMeetingList)
                           setMeetingList();
                       switchState('currentMeetings');
                       if (bbbUserPerms.bbbViewMeetingList)
                            BBBUtils.showMessage(bbb_permissions_saved, 'success');
                   })
                });
               
           BBBUtils.setSitePermissions = function(boxesSelector, successCallback, errorCallback) {
            var boxes = $(boxesSelector);
            var myData = {};
            for(var i=0,j=boxes.length;i<j;i++) {
                var box = boxes[i];
                if(box.checked)
                    myData[box.id] = 'true';
                else
                    myData[box.id] = 'false';
            } */
        $('#courseware_permissions_save_button').click(function(){
            //
            var boxes = $('.courseware_permission_checkbox');
            var myData = {};
            for(var i=0,j=boxes.length;i<j;i++) {
                var box = boxes[i];
                if(box.checked)
                    myData[box.value] = 'true';
                else
                    myData[box.value] = 'false';
            }
            
            jQuery.ajax( {
                url : "/direct/site/${param.siteId}/setPerms",
                type : 'POST',
                data : myData,
                async : false,
                dataType: 'text',
                success : function(data) {
                    if(data) window.location.href='${pageContext.request.contextPath}/cw/index.do';
                },
                error : function(xmlHttpRequest,status,error) {
                    alert("ERROR");               
                }
            });
           //return false;
        });
    });
    </script>
    </html>

  • 相关阅读:
    Webfunny Js错误分析讲解
    Webfunny漏斗分析功能讲解
    Webfunny自定义埋点功能讲解
    Webfunny连线用户功能讲解
    Webfunny用户细查功能讲解
    C语言打印数字前补0
    github上新晋star3K的开源AI模型,包含情感分析等
    IT系统架构的演化
    微服务架构与SOA架构的区别与联系
    开源的分布式事务-Seata的设计原理
  • 原文地址:https://www.cnblogs.com/zrui-xyu/p/4844381.html
Copyright © 2011-2022 走看看