我想通过Jquery AJAX从checkbox中发送数组,但响应不正确。
这里是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">
脚本
$(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();
});
});
当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
)
我想要的是这样的数组,我该如何解决?
Array
(
[0] => 0ec81bdf-1fc6-447d-ab65-bc67a857d99c
[1] => 173867c3-5721-4aa2-9344-f5ad9fd05537
)
答案 0 :(得分:2)
HTML应该是,而不是ID,你必须使用类:
<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">
试试这个脚本:
$(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();
});
});
并打印$_POST
您将获得所需的结果。
答案 1 :(得分:0)
尝试这一个 改变
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
};
到
var formData = $('#form_krs_kolektif').serialize();