Php Ajax和checkbox数组

Php Ajax和checkbox数组,php,jquery,ajax,Php,Jquery,Ajax,我刚刚开始使用ajax,我正在跟随这个链接的教程 但是我没能成功,你能帮我吗 html代码: <html> <head> <script src="jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function doit() {

我刚刚开始使用ajax,我正在跟随这个链接的教程
但是我没能成功,你能帮我吗

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>  

函数doit(){
var p=[];
$('input.cb')。每个(函数(){
if($(this.attr('checked')){
p、 push($(this.attr('rel'));
}
} );
$.ajax({
url:“/test2.php”,
类型:'POST',
数据:{list:p},
成功:功能(res){
警报(res);
}
});
}
这是1
这是abc
这里是999
test2.php:

<?php

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

?>

使用onclick=”“attribute有点不推荐使用,您不应该使用链接来执行操作,而应该使用按钮。链接用于页面,按钮用于操作。 试试这个

$('input.cb').each( function() {
       if($(this).is(':checked')) { // change this line in your code
            p.push($(this).attr('rel'));
       }
 });
if($(this).prop('checked'))
代替
if($(this).attr('checked'))
将解决您的问题 1:你用
这是错误的<代码>
2:您可以选中每个
复选框
您可以选中每个
$('input.cb:checked')
并删除
如果

3:您需要post ajax,所以请使用
$。post

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

函数doit(){
var p=[];
$('input.cb:checked')。每个(函数(索引,值){
p、 push($(value.attr('rel'));
});
$.post('/test2.php',{list:p}).done(函数(res){alert(res);});
}
这是1
这是abc
这里是999

到底是什么不起作用?尝试在函数doit(){之后发出警报,并检查它是否发出警报。@jenz我尝试使用此函数并替换onClick函数,它起作用了函数qwe(){alert('test');}@jenz我需要的是通过数组复选框,所以我看了本教程并尝试了一下,但它不起作用,检查数据是否通过回显该文件中的一些文本传递到test2.php。它正在起作用,使用诸如httpfox或chrome developer之类的工具检查http请求,当然它以错误结尾,因为我没有你的test2.phpOK我爱你!终于成功了!如果可以的话,你能帮我进一步实现我的目标吗?@zxc yes告诉我
:-)
tnx!嗯,我需要将数组复选框传递给test2.php我在test2.php中做的是$list=$\u POST['list'];我在ajax post part上遇到问题嗯,显示时没有错误,但数据库中没有插入数据。我所做的是获取post[LIST],然后将其插入数据库。
print_r(@$_post['LIST']);
或echo是否显示复选框值?我尝试使用此选项并删除此部分$.post('/test2.php',{LIST:p})。完成(函数(res)没有显示警报,因为这未
完成
可能找不到您的php文件,我测试了它,并给出了警报
数组([0]=>1[1]=>abc)
您的php文件必须在web服务器中的html文件旁边,如果不起作用,请在test2.phpok之前更改文件地址并删除
/
。您救了我一天!!!!!如果我可以将此作为aswer检查三次,我会tnx!
<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>