通过ajax将数组传递给php
问题描述:
我有我的页面上显示的不同类型文章的列表。您可以选中或取消选中这些类型,以使其显示/隐藏文章spawner。使用jquery中的当前数组构建器,它无法通过ajax正确传递给PHP中的$ _POST。通过ajax将数组传递给php
我是每次一个复选框取消选中触发功能/检查
$(".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 = {};