douchou8935 2013-01-23 07:48
浏览 475
已采纳

如何使用jQuery获取所有选中复选框的值

I don't know how to pass the selected values of the checkboxes. Any help or suggestions will be a big help for me.

As of now here is my code I am stuck in passing the values of the checkboxes

index.php

<table>
<?php
foreach($response as $item){
    echo '<tr><td><input type="checkbox" value="' .$item['id']. '"></td><td>' . $item['name'] . '</td></tr>';
}
?>
</table>
<button type="button" class="btnadd">AddSelected</button>
<script type="text/javascript">
    $(function() {
        $('.btnadd').click(function() {
            $.ajax({
                url: 'process.php',
                type: 'post',
                data: {  }, // what should I put here to pass the value of checked checkboxes
                success: function(data) {}
            });
        });
    });
</script>

process.php

<?php
$array_ids = $_POST['ids']; // this will retrieve the id's
?>
  • 写回答

6条回答 默认 最新

  • douzi1991 2013-01-23 07:53
    关注

    Try This.

    HTML CODE

    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    
        $(document).ready(function(){
            $('.btnadd').click(function(){
                var checkValues = $('input[name=checkboxlist]:checked').map(function()
                {
                    return $(this).val();
                }).get();
    
                $.ajax({
                    url: 'loadmore.php',
                    type: 'post',
                    data: { ids: checkValues },
                    success:function(data){
    
                    }
                });
            });
        });
    
    </script>
    
    <input type="checkbox" name="checkboxlist" value="1" checked="checked" />
    <input type="checkbox" name="checkboxlist" value="2" checked="checked" />
    <input type="checkbox" name="checkboxlist" value="4" />
    <input type="checkbox" name="checkboxlist" value="5" checked="checked" />
    <input type="checkbox" name="checkboxlist" value="6" />​

    loadmore.php CODE

    <?php
    
        print_r($_POST['ids']);
    
    ?>

    OUTPUT IN loadmore.php

    Array
    (
        [0] => 1
        [1] => 2
        [2] => 5
    )

    That's IT.

    Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?