Jquery通过ajax发布数组

时间:2010-04-22 02:15:17

标签: jquery ajax forms checkbox

我有一个数组(用于复选框),我需要在ajax帖子中传递常规表单,但似乎无法使其工作:

new_data = [a,b,c,d,e];

somedata_assoc = JQuery.param({'choices[]': new_data});

    $.ajax({
        type: "POST",
     url: contract_qurl,
     data: $(div).find("form").serialize()+"&"+somedata_assoc,
     context: $(this),
     success: function(data) { $("#results_table").html(data); }
    });

2 个答案:

答案 0 :(得分:7)

我在这行收到了一个javascript错误

new_data = [a,b,c,d,e];

我不得不把它改成这个

new_data = ['a','b','c','d','e']; 

你在jQuery的j行中大写了J

somedata_assoc = JQuery.param({'choices[]': new_data});

应该是这个(或只是$简写)

somedata_assoc = jQuery.param({'choices': new_data});

另外,我认为你不需要括号,在大多数情况下,它们会使得在服务器上检索数据变得更加困难

答案 1 :(得分:7)

经过研究后,唯一对我有用的解决方案是:

url='url/to/page'
choices = [1,2,3,4,5,6]
    $.post(url,{ 'choices[]': choices }, function(data){
          console.log(data);
    },'html');

另外,使用带括号的'choices',这样你就可以在一个变量中检索服务器,否则它将成为每个数组元素的一个帖子。 这对我有用。您可以看到其他帖子here at stackoverflow

我希望将来可以帮助某人。