通过ajax将数组传递给php

时间:2014-09-29 20:25:23

标签: javascript php jquery arrays ajax

我列出了我在页面上显示的不同类型的文章。您可以选中或取消选中类型,以使其显示/隐藏在文章生成器中。使用我在jquery中的当前数组构建器,它无法通过ajax正确传递到php中的$ _POST。

每次取消选中/选中复选框时触发的我的功能

$(".jquery-checkbox").change(function(){
    var data = new Array();

   //check all the different types and see if they are checked or not
    $(".jquery-checkbox").each(function() {
        //if the type is not checked, put it into the data array to filter from results
        if(!$(this).is(':checked')){
            data[$(this).attr("name")] = $(this).attr("name");
        }
    });

    $.ajax({
            type: "POST",
            url: "/Ajax/article/articleList.php",
            dataType: "html",
            data: data,
            success: function(data) {$("#article-spawner").html(data);},
            error: function(){return false;}
    });
});

是否有我可以找到未检查的每种类型并将它们以这种形式放入数组中:

        var data = {
                "8":"not-checked",
                "10":"not-checked"
        };

它适用于上述方法,但我不确定如何使用.each

来实现此方法

1 个答案:

答案 0 :(得分:0)

在JavaScript中,数组用于带有数字索引的顺序数据。虽然你可以坚持自己喜欢的任何属性,但是大多数处理它们的工具都会忽略这些属性。

这假设输入的名称属性与传统的非数字名称一样。

使用常规对象,而不是数组。

var data = new Array();更改为var data = new Object();var data = {};