简体   繁体   English

通过Jquery AJAX从复选框发布数组

[英]POST Array from checkbox via Jquery AJAX

I want to send array from checkbox via Jquery AJAX, but the response is not right. 我想通过Jquery AJAX从复选框发送数组,但响应不正确。

here the html : 这里的html:

<input type="checkbox" id="krs_id_kelas" name="krs_id_kelas[]" value="0ec81bdf-1fc6-447d-ab65-bc67a857d99c">
<input type="checkbox" id="krs_id_kelas" name="krs_id_kelas[]" value="173867c3-5721-4aa2-9344-f5ad9fd05537">

Script 脚本

$(document).ready(function () {   
$('#form_krs_kolektif').submit(function (event) {

     var formData = {
            'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
            'periode': $('#periode_krs option:selected').val(), //this part is fine
            'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
            'id_kelas[]': $('#krs_id_kelas:checked').serialize() // only this part has a problem
        };
     $.ajax({
            type: 'POST',
            url: '<?=base_url()?>akademik/proses_krs_kolektif/',
            data: formData,
            dataType: 'json',
            encode: true
        })

    event.preventDefault();
    });

}); 

When I print_r the POST result from php part, the response from console is like this 当我print_r php部分的POST结果时,来自控制台的响应是这样的

Array
(
    [0] => krs_id_kelas%5B%5D=0ec81bdf-1fc6-447d-ab65-bc67a857d99c&krs_id_kelas%5B%5D=173867c3-5721-4aa2-9344-f5ad9fd05537
)

What I want is array like this, how can I fix it ? 我想要的是这样的数组,我该如何解决?

Array
(
    [0] => 0ec81bdf-1fc6-447d-ab65-bc67a857d99c
    [1] => 173867c3-5721-4aa2-9344-f5ad9fd05537
)

HTML should be, instead of ID you must use class: 应该使用HTML,而不是ID,您必须使用class:

 <input type="checkbox" class="krs_id_kelas" name="krs_id_kelas[]" value="0ec81bdf-1fc6-447d-ab65-bc67a857d99c">
 <input type="checkbox" class="krs_id_kelas" name="krs_id_kelas[]" value="173867c3-5721-4aa2-9344-f5ad9fd05537">

try this script: 试试这个脚本:

$(document).ready(function () {   
 $('#form_krs_kolektif').submit(function (event) {
 var chekedValue = [];
 $('.krs_id_kelas:checked').each(function(){
   chekedValue .push($(this).val());
 })
 var formData = {
        'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
        'periode': $('#periode_krs option:selected').val(), //this part is fine
        'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
        'id_kelas': chekedValue // only this part has a problem
    };
 $.ajax({
        type: 'POST',
        url: '<?=base_url()?>akademik/proses_krs_kolektif/',
        data: formData,
        dataType: 'json',
        encode: true
    })

event.preventDefault();
});

}); 

and print $_POST you will get the desired result. 并打印$_POST您将获得所需的结果。

try this One 试试这个
change from

var formData = {
    'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
    'periode': $('#periode_krs option:selected').val(), //this part is fine
    'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
    'id_kelas[]': $('#krs_id_kelas:checked').serialize() // only this part has a problem
};

to

var formData = $('#form_krs_kolektif').serialize();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM