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条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退