簡體   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