dp9599 2017-07-18 04:35
浏览 70
已采纳

通过Jquery AJAX复选框中的POST数组

I want to send array from checkbox via Jquery AJAX, but the response is not right.

here the 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

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
)
  • 写回答

2条回答 默认 最新

  • dqe9657 2017-07-18 04:55
    关注

    HTML should be, instead of ID you must use 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!