【问题标题】:Get an id on checkbox在复选框上获取 id
【发布时间】:2017-10-01 08:05:50
【问题描述】:

我有这样的html代码。

HTML

<div class="container" style="padding-top: 30px; padding-bottom: 30px; width: 1089px;">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">

            <div class="fresh-table toolbar-color-orange">
            <!--    Available colors for the full background: full-color-blue, full-color-azure, full-color-green, full-color-red, full-color-orange                  
                    Available colors only for the toolbar: toolbar-color-blue, toolbar-color-azure, toolbar-color-green, toolbar-color-red, toolbar-color-orange
            -->

                <div class="toolbar">

                  <button id="selectAll" class="btn btn-default" style="padding-right:70px;">Select All</button>

                    <button id="popup" onclick="div_show()" class="btn btn-default" style="margin-left: 650px;">Send</button>
                </div>

                <table id="fresh-table1" class="table1">
                    <thead>
                       <tr>   
                       <th></th>  
                          <th data-field="id" data-sortable="true">ID</th>
                        <th data-field="name" data-sortable="true">First Name</th>
                        <th data-field="salary" data-sortable="true">Last Name</th>
                        <th data-field="country" data-sortable="true">Date Of Birth</th>
                        <th data-field="city">Gender</th>
                        <!-- <th data-field="actions" data-formatter="operateFormatter1" data-events="operateEvents1">Actions</th> -->
   </tr>           
                    </thead>

                    <tbody>
                            <tr>
                          <?php  
                          foreach ($user->result_array () as $row ) {?>

                          <td><input type="checkbox"></td>
                              <td><?php  echo $row['user_id'];?></td>
                                <td><?php  echo $row['firstname'];?></td>
                                <td><?php  echo $row['lastname'];?></td>
                                <td><?php  echo $row['dob'];?></td>
                                 <td><?php if($row['gender']==1){ echo                        'Male';}else{echo 'Female';}?></td> 

                            </tr>           
   <?php }?>                          
                    </tbody>
                </table>
            </div>             
        </div>
 </div>
    </div>

我的 javascript 就是这样。

Javascript

  <script>

    $(document).ready(function () {
    $('body').on('click', '#selectAll', function () {

    if ($(this).hasClass('allChecked')) {
        $('input[type="checkbox"]', '#fresh-table1').prop('checked', false);
    } else {
        $('input[type="checkbox"]', '#fresh-table1').prop('checked', true);
    }
    $(this).toggleClass('allChecked');
  })
});

我正在使用 Codeigniter 框架。我在表格中使用了 javascript。我 想在选择按钮上获得user_id,以便我可以将通知发送到选定的 ID。我想要数组格式的user_id

【问题讨论】:

    标签: javascript php mysql codeigniter


    【解决方案1】:

    :checkbox:checked 选择器和map 创建一个 user_ids 数组:

    下面是工作演示:

    function getAllCheckedUserIds() {
      var userIds = $('input:checkbox:checked').map(function() {
        return $(this).parent().next().text();
      }).get();
      var userIds = userIds.filter(function(v) {
        return v !== ''
      });
      console.log(userIds);
      return userIds;
    }
    
    $(document).ready(function() {
      $('body').on('click', '#selectAll', function() {
        if ($(this).is(":checked")) {
          $('input[type="checkbox"]', '#fresh-table1').prop('checked', true);
          getAllCheckedUserIds();
        } else {
          $('input[type="checkbox"]', '#fresh-table1').prop('checked', false);
        }
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input id="selectAll" type='checkbox' class="allChecked" /> select All
    
    <table id='fresh-table1'>
      <tr>
        <td><input type='checkbox' /></td>
        <td>User id: 1</td>
      </tr>
      <tr>
        <td><input type='checkbox' /></td>
        <td>User id: 2</td>
      </tr>
    </table>

    【讨论】:

    • 获取类型错误:this.parent 不是函数错误
    • 您可以将此代码添加到一个函数中,并在您想要的任何位置调用该函数。这应该返回一个 user_ids 数组。在那个函数里面,最后,你应该有return userIds
    • @kalpita - 你可以试试$(this).parent()
    • 使用 $(this).parent() 在控制台上没有得到任何东西
    • 我已经更新了代码,也有功能。你可以试试。检查控制台中的 user_ids 数组
    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 2018-08-02
    • 1970-01-01
    • 2018-02-19
    • 2011-12-10
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多