如何使用jQuery获取所有选中复选框的值 [英] How to get the values of all checked checkboxes using jQuery

查看:24
本文介绍了如何使用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

<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
?>

推荐答案

试试这个.

HTML 代码

<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 代码

<?php

    print_r($_POST['ids']);

?>

在 loadmore.php 中输出

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

就是这样.

干杯.

这篇关于如何使用jQuery获取所有选中复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆