从 jQuery 中复选框的数组值中删除

Ogn*_*j3n 4 javascript arrays checkbox jquery

我有一个这样的复选框:

while($userRow=$resultForUsers->fetch_assoc()){
      $nameOfUser=$userRow['users_name'];
      $userId=$userRow['user_facebook_id'];
      $userFBPicture=$userRow['users_picture'];
      echo "
      <tr class='tr-data-users'>
      <td class='text-center'>
      <input type='checkbox' class='checkbox' onclick='if(this.checked){selo(this.value)}else{izbaci(this.value)}' value=$userId>
      </td>
Run Code Online (Sandbox Code Playgroud)

因此,对于我数据库中的每个用户,我都有一个复选框,其中包含他的 id 值。我需要一个数组中所有选中用户(即复选框)的 ID。我是这样做的:

<input type='checkbox' class='checkbox' onclick='if(this.checked){put(this.value)}else{remove(this.value)}' value=$userId>
 var  niz={};
    var index=0;
        function put(o){
            niz[index++]=o;
            console.log(niz);
        }
Run Code Online (Sandbox Code Playgroud)

因此,console.log现在显示所有选中复选框的 ID。我想要做的是,如果未选中复选框,则从数组中删除该 id(即 chechbox 值)。我是这样试的:

onclick='if(this.checked){put(this.value)}else{remove(this.value)}'
 var  niz={};
    var index=0;
        function put(o){
            niz[index++]=o;
            console.log(niz);
            remove(o,niz);
        }

        function remove(o,niz){
            if($.inArray(o,niz)){
                console.log('radim');
                var indexNiza= $.inArray(o,niz);
               niz= $.grep(niz,function(a,o){
                   return (a!=o);
               });
            }
        }   
Run Code Online (Sandbox Code Playgroud)

如您所见,else如果未选中复选框并从数组中删除该 id,这部分应该处理,但它不起作用。真的很感激这方面的帮助。

Kra*_*ime 5

您编写的代码似乎为一项简单的工作采取了非常复杂的路线。您可以查看以下示例,了解如何将所有值获取到它们自己的checkedunchecked状态数组中。

在演示中,如果用户更改了任何复选框的状态,我将枚举选中和未选中的复选框,并将cbChecked选中的值存储在名为的数组中,未选中的值存储在cbUnchecked

这里的关键是使用的选择器:

选择器用法

获取所有“已检查”对象

:checked
Run Code Online (Sandbox Code Playgroud)

获取所有“未选中”的对象

:not(:checked)
Run Code Online (Sandbox Code Playgroud)

示范