2014-09-29 211 views
0

我有我的頁面上顯示的不同類型文章的列表。您可以選中或取消選中這些類型,以使其顯示/隱藏文章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

回答

0

在JavaScript中,數組用於具有數字索引的順序數據。雖然你可以將任何你喜歡的屬性粘貼到它們上面,但是大多數處理它們的工具都會忽略這些屬性。

這假設輸入的名稱屬性與傳統的非數字名稱一樣。使用常規對象而不是數組。

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