【发布时间】:2016-01-18 08:49:00
【问题描述】:
当我在 AJAX 中调用函数时,显示消息错误“未定义索引:id”,但如果在视图中我有一个按钮,它可以工作,但是当我有两个按钮时显示此错误。有什么问题?我需要为此创建一个数组或任何解决方案吗?
<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;
});
【问题讨论】:
-
在浏览器控制台中使用使用 firebug 的检查元素来检查 id 值是否存在。然后在 js 代码中提醒你 id。
标签: javascript php ajax codeigniter