2012-02-15 42 views
1

我有一個複選框數組檢查[],我需要通過一個JQuery Ajax請求傳遞給另一個PHP文件。我希望接收的php文件能夠像其他任何POST變量一樣接收數組,而不需要使用Ajax作爲$ _POST ['check']。JQuery/AJAX後複選框陣列

我是否需要使用JQuery將這些數據解析爲數組?

回答

0
$(document).ready(function(){ 
    //when the form is submitted 
    $("form").submit(function(e){ 
    //prevent the form from actually submitting. 
    e.preventDefault(); 
    //put your PHP URL in here.. 
    var url = "postToThisURL"; 
    //create empty object 
    var obj = {}; 
    //grab the checkboxes and put in arr 
    var arr = $(this).serializeArray(); 
    //iterate over the array and change it into an object 
    for (var i = 0; i < arr.length; ++i) obj[arr[i].name] = arr[i].value; 
    //post the data to the server 
    $.post(url, obj, function(r){ 
     //log the response when it is complete. 
     console.log(r); 
    }); 
    }); 
});