簡體   English   中英

JQuery / AJAX發布復選框數組

[英]JQuery/AJAX post checkbox array

我有一個Checkbox數組check [],我需要通過JQuery Ajax請求將其傳遞給另一個php文件。 我希望接收的php文件能夠像處理其他任何POST變量一樣拾取數組,以及不使用Ajax作為$ _POST ['check']的方式。

我是否需要使用JQuery將此數據解析為數組?

您可以使用http://api.jquery.com/serialize/從復選框中創建一個url編碼的字符串。

$(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);
    });
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM