如何使用jQuery通过Ajax发送一个复选框数组的值?

时间:2022-09-11 13:42:15

I have a form with a lot of form fields (12 x n rows). The first field in each row (which represents a product) is a checkbox that resembles this:

我有一个包含很多表单字段(12 x n行)的表单。每一行中的第一个字段(代表一个产品)是一个类似于这个的复选框:

<input type="checkbox" class="ids" name="ids[]" value="1">

The value of each checkbox is unique.

每个复选框的值都是唯一的。

What I am trying to do is send checked values to a PHP script for processing via Ajax. What I am am having issues with is getting the IDs to the server properly. I have tried using several things including:

我要做的是将检查过的值发送到PHP脚本,以便通过Ajax进行处理。我所遇到的问题是如何正确地将IDs发送到服务器。我试过用以下几种方法:

$('.ids:checked').serialize();

and

var ids = [];
$('.ids:checked').each(function(i, e) {
    ids.push($(this).val());
});

$.ajax({
    url: "stub",
    type: "post",
    dataType: "json",
    data: {
        'ids[]': 'ids[]='+ids.join('&ids[]=')
    },
    success: function(data) {
        // stub
    }
});

But these both result in getting this on the server:

但这两种方法都能在服务器上实现:

ids[]=104&ids;[]=105

I could serialize the whole form and send it over but that could result in a lot of data being sent that is going to be unused.

我可以序列化整个表单并将其发送过来,但这会导致大量数据被发送,而这些数据将会被闲置。

How do I send only the values of delete[] to the server? Ideally in a way that PHP recognizes it as an array?

如何只将delete[]的值发送到服务器?最理想的情况是PHP将它识别为数组?

(I have worked around it by sending the IDs over as a comma delimited string but would like to know how to accomplish this since I spent enough time trying to figure it out).

(我通过将id作为逗号分隔的字符串发送过来来解决这个问题,但是我想知道如何完成这个任务,因为我花了足够的时间来尝试解决它)。

2 个解决方案

#1


16  

This worked fine for me

这对我很有效

<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">

<div id="response"></div>
<button id="submit">Submit</button>

<script>

$('#submit').click(function() {

$.ajax({
    url: "stub.php",
    type: "post",
    data: $('.ids:checked').serialize(),
    success: function(data) {
    $('#response').html(data);
    }
});


});
</script>

Then on stub.php

然后在stub.php

var_dump($_POST);

#2


2  

Why don't you send the id's as comma separated string. You can split it on server side and apply the logic associated with it..

为什么不以逗号分隔的字符串形式发送id呢?您可以在服务器端对其进行分割,并应用与之关联的逻辑。

var ids = [];
$('.ids:checked').each(function(i, e) {
    ids.push($(this).val());
});

$.ajax({
    url: "stub",
    type: "post",
    dataType: "json",
    data: {
        'ids[]': ids.join()
    },
    success: function(data) {
        // stub
    }
});

#1


16  

This worked fine for me

这对我很有效

<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
<input type="checkbox" class="ids" name="ids[]" value="5">
<input type="checkbox" class="ids" name="ids[]" value="6">

<div id="response"></div>
<button id="submit">Submit</button>

<script>

$('#submit').click(function() {

$.ajax({
    url: "stub.php",
    type: "post",
    data: $('.ids:checked').serialize(),
    success: function(data) {
    $('#response').html(data);
    }
});


});
</script>

Then on stub.php

然后在stub.php

var_dump($_POST);

#2


2  

Why don't you send the id's as comma separated string. You can split it on server side and apply the logic associated with it..

为什么不以逗号分隔的字符串形式发送id呢?您可以在服务器端对其进行分割,并应用与之关联的逻辑。

var ids = [];
$('.ids:checked').each(function(i, e) {
    ids.push($(this).val());
});

$.ajax({
    url: "stub",
    type: "post",
    dataType: "json",
    data: {
        'ids[]': ids.join()
    },
    success: function(data) {
        // stub
    }
});