我有一个包含选定产品和图片的页面。我想从数据库和文件夹中删除有关图片的数据,并通过 jQuery ajax 更新页面。
首先我尝试使用它<a href>
,我可以成功删除一张更新页面的图片,但是在 URL 更改后(末尾添加了'#'),因此脚本停止发送 GET(这部分已注释,您可以在下面找到)
其次,我添加了按钮并使用.click()
,然后.on
是事件。也是第一张图片第二次不删除,刷新后才删除。第二次尝试 - 没有任何变化,就像没有事件的按钮。
控制器 main.php:
function del_image($picture_id){
$this->load->model('admin/Product_crud');
$data['picture_name'] = $this->Product_crud->get_picture_name($picture_id);
//var_dump($data);
unlink('uploads/100/'.$data['picture_name'][0]->url);
$data['product_id'] = $this->Product_crud->find_product_id_from_picture($picture_id);
$this->Product_crud->del_picture_from_db($picture_id);
$data['picture'] = $this->Product_crud->get_picture($data['product_id'][0]->product_id);
$update_html = $this->Product_crud->create_picture_output($data['picture']);
echo json_encode($update_html);
//$this->output->enable_profiler(TRUE);
}
模型 product_crud.php:
function create_picture_output($pictures){
$output = NULL;
$output .="<table border='1'>";
foreach ($pictures as $picture){
$output .= "<tr>";
$output .= "<td><a class='del_picture' href='".$picture->picture_id."'><img title='Удалить' src='".base_url()."images/del.png'> </a></td>
<td><img src='".base_url()."uploads/100/".$picture->url."'></td>
<td><input class='del_picture' type='button' name='del' value='".$picture->picture_id."'><img title='Удалить' src='".base_url()."images/del.png'> </td>";
$output .= "</tr>";
}
$output .="</table>";
return $output;
}
查看 form_edit_product.php ......
echo "<div id='result_table'>";
echo "<table border='1'>";
foreach ($url as $picture){
echo "<tr>";
echo "<td><a class='del_picture' href='".$picture_id[$l]."'><img title='Удалить' src='".base_url()."images/del.png'></a></td>
<td><img src='".base_url()."uploads/100/".$picture."'></td>
<td><input class='del_picture' type='button' name='del' value='".$picture_id[$l]."'><img title='Удалить' src='".base_url()."images/del.png'></td>";
$l++;
echo "</tr>";
}
?>
</table>
</div>
脚本:
$(".del_picture").on("click", function(){
/* var picture_id = $(this).attr("href");
var myurl = '<?php echo base_url()."admin/main/del_image/"?>';
$(this).attr("href",'#'); */
var picture_id = $(this).attr("value");
var myurl = '<?php echo base_url()."admin/main/del_image/"?>';
var url = myurl + picture_id
$.ajax({
url: url,
type:'GET',
dataType: 'json',
success: function(update_html){
$("#result_table").html(update_html);
}
});
});
问题出在 URL/jQuery 脚本/GET 的某个地方。可能是在更新事件后未添加到按钮...