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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂