繁体   English   中英

选择多个复选框并将值分配给数组-HTML / Javascript

[英]Selecting multiple checkboxes and assigning values into array - HTML/Javascript

我有一个模式对话框,在名称旁边显示一个复选框表。 我想分配这些复选框以选择该名称的值(即,当我在表上选中“ Jon”旁边的框时,我想选择该名称)。 当我单击多个复选框时,我想将其作为数组传递给javascript。

本质上:单击名称旁边的复选框->选择的名称->在对话框上按“确定”->将数组中名称的ID发送给JS代码

HTML代码:

</div>
           <div class="modal-body">
                <table class="table table-striped table-hover ">
                <thead>
                    <tr>
                        <th>Select</th>
                        <th>User Name</th>                       
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="user in users" ng-hide="user.name === lic.users.name">
                        <td><input type="checkbox" ng-model="user.selected" id="userId"/></td>
                        <td>{{user.name}}</td>
                    </tr> 
                </tbody>
                </table>
           </div>
           <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary btn-sm" ng-click="addLicUser(userId, lic.license.id)">Ok</button>
           </div>
       </div>


<div class="modal" id="add-new-lic-user">
   <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
        <h4 class="modal-title">Add License User</h4>
        </div>
           <div class="modal-body">
                <p>Are you sure you want to add these users to this license?</p>
           </div>
           <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary btn-sm" ng-click="processLicenseUser">Ok</button>
           </div>
       </div>
   </div>

Javascript:

 $scope.addLicenseUser = function (){
  $("#add-lic-user").modal('show');
};

  $scope.addLicUser = function (usId, licId){
  $scope.licenseId = licId;
  $scope.userIdToAdd = usId;
  $("#add-lic-user").modal('hide');
  $("#add-new-lic-user").modal('show');
    };

  $("#processLicenseUser").click(function(e){
  e.preventDefault();
  $scope.processLicenseUser();

});

$scope.processLicenseUser = function () {

  usId.forEach(function(entry){
    //
   var req = {
            method: 'POST',
            url: '/rest/v1/porta/mapping',
            headers:{
                'Content-Type': 'application/json',
                'GoverlanHeader_1': 'Yes',
                'x-access-token': $scope.authInfo.token
            },
            data: {'userId': $scope.userIdToAdd, 'licenseId': $scope.licenseId}
        };

您已经绑定到每个用户的选定属性,可以通过以下方式进行简单过滤

$scope.processLicenseUser = function () {
  var selected_users = [];
  $scope.users.forEach(function(user) {
     if user.selected {
       selected_users.push({'userId': user.Id, 'licenseId': user.licenseId});
     }
   })

   var req = {
        method: 'POST',
        url: '/rest/v1/porta/mapping',
        headers:{
            'Content-Type': 'application/json',
            'GoverlanHeader_1': 'Yes',
            'x-access-token': $scope.authInfo.token
        },
        data: selected_users
    };

“我想将其作为数组传递给javascript。”

您没有在代码中的任何位置将单击的项目推入数组。

$scope.users = [];

  $scope.checked = function() {
    $scope.user.splice(0, $scope.user.length); 
    $scope.user.push('id');
  };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM