当我在 AJAX 中调用函数时,显示消息错误“未定义索引:id”,但如果在 View 中我有一个按钮,则可以工作,但当我有两个按钮时,会显示此错误。有什么问题?我需要为此创建一个数组或任何解决方案吗?
<tbody>
<?php foreach ($fotos_concurso as $fotos) { ?>
<tr>
<td><img width="200" height="200" src="<?php echo base_url('/uploads/'.$fotos['nombre_archivo']); ?>"></td>
<td><?php echo $fotos['nombre']; ?></td>
<td><a class="aprobar_foto" href="<?= base_url('admin/aprobar_foto') ?>" id="<?php echo $fotos['id'] ?>" title="aprobar"><i class="icon-trash"></i></a></td>
<td><a class="desaprobar_foto" href="<?= base_url('admin/desaprobar_foto') ?>" id="<?php echo $fotos['id'] ?>" title="desaprobar"><i class="icon-trash"></i></a></td>
</tr>
<?php } ?>
</tbody>
以及 AJAX 代码
$('.aprobar_foto').on('click', function() {
q = $(this);
if (confirm('¿Quieres aprobar la foto?')) {
$url = q.attr('href');
$.ajax({
type: "post",
url: $url,
cache: false,
data: "id=" + q.attr('id'),
fail: function () {
alert('Ha habido un error al borrar el registro');
},
success: function (redir) {
window.location = redir;
}
});
}
return false;
});
$('.desaprobar_foto').on('click', function() {
q = $(this);
if (confirm('¿Quieres desaprobar la foto?')) {
$url = q.attr('href');
$.ajax({
type: "post",
url: $url,
cache: false,
data: "id=" + q.attr('id'),
fail: function () {
alert('Ha habido un error al borrar el registro');
},
success: function (redir) {
window.location = redir;
}
});
}
return false;
});
最佳答案
像这样设置你的 id 字段:
var id = q.attr('id');
data: {id: id},
关于javascript - 未定义索引 : id in AJAX, 代码点火器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34850512/