通过ajax将数组传递给php

Passing array to php through ajax

本文关键字:php 数组 ajax 通过      更新时间:2023-09-26

我在页面上显示了不同类型的文章列表。您可以选中或取消选中类型,以使它们在文章spawner中显示/隐藏。对于我目前在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 来达到这个方法

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

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

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

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