返回单击时选中的所有复选框的数组



我有一个表,每行都有一个复选框,如下所示:

<table id='users'>
    <thead>
    ...
    </thead>
    <tbody>
        <tr>
            <td><input type='checkbox' name='users' id='someUserId'></td>
            <td> some variable pid </td>
            <td>...</td>
        </tr>
        <tr>
            <td><input type='checkbox' name='users' id='someOtherId'></td>
            <td> some other variable pid </td>
            <td>...</td>
        </tr>
        ...
    </tbody>
</table>

现在,我想将复选框(pid)旁边的列的文本放入一个数组中,然后将数组传递给一个函数。函数应该获取数组中的每个记录并对其进行处理。

我迄今为止最好的尝试:

function myFunction(arr[]){...}
function getIds(obj){
var $table = $("#users");
alert($table.attr("id"));
var $cboxes = $table.find("input:checkbox").toArray();
alert($cboxes);
var checkedArray = [];
var pid;
for(i = 0;i < $cboxes.length; i++){
    if($cboxes[i].checked){
        pid = $cboxes.parent().siblings().eq(0).text();
        checkedArray.push(pid);
        alert(pid);
    }
}
alert(checkedArray);
return checkedArray;
}
$("#button").click(function(){
    var ids = getIds();
    for(i = 0; i < ids.length; i++){
        myFunction(ids[i]);
        alert("Function executed for "+ids[i]+".");
    }
});

您可以使用:checked伪选择器和$.map来大幅精简。

function process (id) {
  alert(id);
}
function find () {
  return $('#users').find('input:checkbox:checked').map(function () {
    return $(this).parent().next().text();
  }).toArray();
}
function handle () {
  find().forEach(process);
}
$('#btn').on('click', handle); // Pseudo-event
<table id='users'>
  <thead>
  </thead>
  <tbody>
    <tr>
      <td><input type='checkbox' name='users' id='someUserId'></td>
      <td> some variable pid </td>
      <td>...</td>
    </tr>
    <tr>
      <td><input type='checkbox' name='users' id='someOtherId'></td>
      <td> some other variable pid </td>
      <td>...</td>
    </tr>
  </tbody>
</table>
<button id="btn">Check!</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

如果你不想循环两次,你应该把逻辑压缩成一个单一的函数链。这样可以减少循环,但仍可以构建阵列以供以后使用。

var myNamespace = {};
function process (id) {
  alert('Single ID: '+ id);
  return id;
}
function find () {
  return $('#users').find('input:checkbox:checked').map(function () {
    return process($(this).parent().next().text());
  }).toArray();
}
function handle () {
  myNamespace.ids = find();
  alert('All IDs: ' + myNamespace.ids)
}
$('#btn').on('click', handle); // Pseudo-event
<table id='users'>
  <thead>
  </thead>
  <tbody>
    <tr>
      <td><input type='checkbox' name='users' id='someUserId'></td>
      <td> some variable pid </td>
      <td>...</td>
    </tr>
    <tr>
      <td><input type='checkbox' name='users' id='someOtherId'></td>
      <td> some other variable pid </td>
      <td>...</td>
    </tr>
  </tbody>
</table>
<button id="btn">Check!</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

试试这个:

小提琴

 function getIds() {
     var chk = $("#users").find("input:checkbox");
     var pId = [];
     chk.each(function(){
         pId.push($(this).parent().next().html());
     });
     return pId;
 }
 $(function(){
     getIds().forEach(function(i){
         console.log(i);
     });
 });

找到users表中的所有复选框,创建数组,将所有pId推送到数组中,然后从函数中返回。然后把它们全部循环一遍。

最新更新