duan0531 2017-10-04 18:40
浏览 54
已采纳

点击按钮获取所有选中的行

i have a table in which data populating dynamically from db, now there is a checkbox on start of each row,what i want to do is when the user clicks on export button i want to get all those rows which are checked, this is my code!

Dyanamic Table

<table id="example" class="export_table">
                        <thead>
                        <tr>
                            <th id="not1">Check Box</th>
                            <th>Brand</th>
                            <th>Production Date</th>
                            <th>Expiry Date</th>
                            <th>Made In</th>
                            <th>Status</th>
                            <th>Batch Code</th>
                            <th>Created On</th>
                        </tr>
                        </thead>
                        <tbody>

                        $i=0;
                        foreach($results as $r){
                            echo "<tr>";
                            echo "<th id='not2'><input type='checkbox' name='checkedvalues' value='val_".$i."'/> </th>";
                            echo "<td>".$r['brand']."</td>";
                            echo "<td>".$r['production_date']."</td>";
                            echo "<td>".$r['expiry_date']."</td>";
                            echo "<td>".$r['madein']."</td>";
                            echo "<td>".$r['status']."</td>";
                            echo "<td id='main-print'>".$r['batch_code']."</td>";
                            echo "<td>".$r['created_on']."</td>";
                            echo "</tr>";
                            $i++;

                        }

                        ?>

                        </tbody>
                    </table>

JQuery

$(document).ready(function() {

        $("#export").click(function(){

            var selectedvalues = [];

            $.each($("input[name='checkedvalues']:checked"), function(){

                selectedvalues.push($(this).val());

            });

            alert("Checked values are: " + selectedvalues.join(", "));

        });

    });

now i am getting only the values of selected checkbox which infact i dont want what i want is to generate an array which should be populated on the basis of checked attributes! it should take the full row on the basis of which checkbox is checked and then return me an array with all rows which are checked, any help?

  • 写回答

1条回答 默认 最新

  • doulu0266 2017-10-04 18:52
    关注

    You can store the row data as an object in "value" in each checkbox. Something like below (inside your foreach loop)

    foreach($results as $r){
            $obj = "{".$r['brand'].",".$r['production_date'].",".$r['expiry_date'].",".$r['madein'].",".$r['status'].",".$r['batch_code'].",".$r['created_on']."}";
            echo "<tr>";
            echo "<th id='not2'><input type='checkbox' name='checkedvalues' value='".$obj."'/> </th>";
            ...
            ...
    }
    

    Now, your selectedvalues array will have javascript objects with each checked row.

    p.s sorry for any php syntax errors

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题