Ajax和checkbox数组

时间:2014-04-07 11:47:21

标签: php jquery ajax

我只是使用ajax的新手,我正在关注此链接http://ricochen.wordpress.com/2011/08/02/passing-array-of-checkbox-values-to-php-through-jquery-example/的教程 但是我不能让它工作你能帮我解决这个问题吗?

html代码:

<html>
<head>
<script src="jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> 
 <script type="text/javascript">
                function doit() {
                        var p=[];
                        $('input.cb').each( function() {
                                if($(this).attr('checked')) {
                                        p.push($(this).attr('rel'));
                                }
                        } );
                        $.ajax( {
                                url:'/test2.php',
                                type:'POST',
                                data: {list:p},
                                success: function(res) {
                                        alert(res);
                                }
                        });
                }
        </script>
</head>
<body>       
 <input type="checkbox" class="cb" rel="1"></input>This is 1<br />
        <input type="checkbox" class="cb" rel="abc"></input>This is abc<br />
        <input type="checkbox" class="cb" rel="999"></input>This is 999<br />
        <a href="javascript:void(0)" onclick="doit()">Click</a>       
</body>
</html>  

test2.php:

<?php

        print_r(@$_POST['list']);

?>

4 个答案:

答案 0 :(得分:1)

试试这个

$('input.cb').each( function() {
       if($(this).is(':checked')) { // change this line in your code
            p.push($(this).attr('rel'));
       }
 });

DEMO

答案 1 :(得分:1)

试试这个:
1:你使用<input></input>这是错误的! <input type='checkbox' name='' />
2:你们每人checkbox $('input.cb:checked') if并删除$.post 3:你想要发布ajax,所以请使用<html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function doit() { var p=[]; $('input.cb:checked').each(function(index,value) { p.push($(value).attr('rel')); }); $.post('/test2.php',{list:p}).done(function(res) {alert(res);}); } </script> </head> <body> <input type="checkbox" class="cb" rel="1"/>This is 1<br /> <input type="checkbox" class="cb" rel="abc"/>This is abc<br /> <input type="checkbox" class="cb" rel="999"/>This is 999<br /> <a href="javascript:void(0)" onclick="doit()">Click</a> </body> </html>

{{1}}

答案 2 :(得分:0)

使用onclick =&#34;&#34; attribut有点被弃用+您应该使用链接进行操作,您应该使用按钮。链接用于页面和操作按钮。 http://jsfiddle.net/tLq8L/1/&lt; - 在此处查看工作版本

 <script>
 function doit() {

                    var p=[];
                    $('input.cb').each( function() {
                            if($(this).attr('checked')) {
                                    p.push($(this).attr('rel'));
                            }
                    } );
                    $.ajax( {
                            url:'/test2.php',
                            type:'POST',
                            data: {list:p},
                            success: function(res) {
                                    alert(res);
                            }
                    });
            }
 </script>
  <input type="checkbox" class="cb" rel="1"></input>This is 1<br />
    <input type="checkbox" class="cb" rel="abc"></input>This is abc<br />
    <input type="checkbox" class="cb" rel="999"></input>This is 999<br />
    <a href="javascript:void(0)" onclick="doit()">Click</a>       

答案 3 :(得分:0)

if($(this).prop('checked'))代替if($(this).attr('checked'))将解决您的问题

相关问题