我更改了一些默认的opencart管理产品列表过滤功能,如下所示:如果我需要从列表中删除产品我替换复选框,单击每个产品删除图标,此处附带截图...
它的工作正常,但如何设置一个jquery功能与对话框确认删除?,就像我们默认的opencart产品删除确认...当点击删除按钮询问确认删除?与确认和取消按钮对话框...任何想法?
我的产品删除按钮,其中包含html中的列表:
<?php if ($products) { ?>
<?php foreach ($products as $result) { ?>
<div class="product">
<a href="<?php echo $result['delete']; ?>"><img src="delete.png" title="<?php echo $button_delete; ?>" /></a>
<img src="<?php echo $result['image']; ?>" alt="<?php echo $result['name']; ?>"/>
<a href="<?php echo $result['view']; ?>"><?php echo $result['name']; ?></a>
</div>
<?php } ?>
<div class="pagination"></div>
<?php } else { ?>
<div class="empty"><?php echo $text_empty; ?></div>
<?php } ?>
从上面的代码删除按钮:
<a href="<?php echo $result['delete']; ?>"><img src="delete.png" title="<?php echo $button_delete; ?>" /></a>
有什么想法吗?
答案 0 :(得分:1)
非jQuery
HTML:
<a href="#" onclick='confirm_delete("<?php echo $result['delete']; ?>", "<?php echo $result['name']; ?>"); return false;'>
<img src="delete.png" title="<?php echo $button_delete; ?>" />
</a>
Javascript:
<script>
function confirm_delete(link_to_delete, product_name) {
var msg=confirm("Are you sure you want to delete " + product_name + " ?");
if (msg==true)
{
window.location.href = link_to_delete;
}
}
</script>
<强>的jQuery 强>
HTML:
<a class="delete_button" href="<?php echo $result['delete']; ?>">
<img src="delete.png" title="<?php echo $button_delete; ?>" />
</a>
javascript(jQuery):
<script>
jQuery(".delete_button").on("click", function(e) {
e.preventDefault();
var clicked = jQuery(this);
var clicked_url = clicked.attr("href");
var product_name = clicked.siblings("img").attr("alt");
var msg=confirm("Are you sure you want to delete " + product_name + " ?");
if (msg==true)
{
window.location.href = clicked_url;
}
});
</script>
答案 1 :(得分:0)
您可以使用jQuery:
$('div.product a img[src="delete.png"]').on('click', function(event){
if( ! confirm('Delete?')) {
event.preventDefault();
return false;
}
return true;
});
答案 2 :(得分:0)
这是我如何在一行项目上完成的,每一项都被称为&#34; offer。&#34;这是模板代码:
<a href="<?php echo $offer['delete']; ?>" data-toggle="tooltip" title="<?php echo $button_delete; ?>" class="btn btn-primary" onclick="confirm('<?php echo $text_confirm; ?>') ? $('#form-discount_chooser').submit() : false;"><i class="fa fa-trash-o"></i></a>
在调用模板之前,您的控制器会按如下方式设置$offer['delete']
:
$data['offers'][] = array(
...
'delete' => $this->url->link('extension/module/discount_chooser/delete', 'token=' . $this->session->data['token'] . '&offer=' . $discount['id'] . $url, true)
);
然后在控制器的delete()方法中,一定要处理这是一个项目的事实,而不是一个或多个复选框:
// Handle onesies
if (!isset($this->request->post['selected'])) {
if (isset($this->request->get['offer'])) {
$this->request->post['selected'] = array($this->request->get['offer']);
}
}