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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12433438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:01  来源:igfitidea点击:

How to send the values of an array of checkboxes through Ajax using jQuery?

phpjqueryajaxarrays

提问by John Conde

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 xn 行)的表单。每行的第一个字段(代表一个产品)是一个复选框,类似于:

<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 进行处理。我遇到的问题是正确获取服务器的 ID。我尝试过使用多种方法,包括:

$('.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 作为逗号分隔的字符串发送来解决了这个问题,但我想知道如何实现这一点,因为我花了足够的时间试图弄清楚)。

回答by Kris

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);

回答by Sushanth --

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

回答by sandeep autade

Solution for me working fine

我的解决方案工作正常

//get checkbox checked data as the array

var ids = new Array();
      $('input[name="ids[]"]:checked').each(function(){
         ids.push($(this).val());
      });



var dataString = 'ids='+ ids;


 $.ajax({
            type: "POST",
            url: "ajax_fees_pay_Directly.php",
            data: dataString,
            cache: false,
            success: function(data){

}
 });