目标
我有一个带有物品列表的网页。每个物品旁边都有一个删除按钮。当用户点击该按钮时,我想要:
- 询问用户确认
- 从数据库中删除相应的物品
- 从列表中删除该物品的行
当前解决方案
目前,我正在执行类似于以下操作:
$('button.delete').click(function(){
thisRow = $(this).parent();
itemID = $(this).parent().attr('id');
if (confirm('Are you sure?')){
$.post('/manage_items.php', {"action":"delete", "itemid":itemID}, function(){
thisRow.hide("slow").remove();
});
}
}
这个解决方案可行是因为每个都可以确定它属于哪一行和项目,并相应地执行操作。
期望的解决方案
不想使用笨重的“确定”或“取消”警告框,我想使用jQuery UI对话框。但我不确定如何让对话框知道在任何给定点击时它应该处理哪一行和哪个项目。
这是如何设置:
1)定义一个对话框div
<div class="dialogbox" id="confirmdeleteitem" title="Really DELETE this item?">
<p>Gee golly, are you s-s-s-sure you want to do that?!</p>
</div>
2) 设置对话框行为
$('#cofirmdeleteitem').dialog({
//other options - not relevant here
buttons: {
"Nevermind": function() {
//do nothing
},
"Alright! Woo!": function(){
//do something
}
}
});
3) 设置打开对话框的点击事件
$('button.delete').click(function(){
$('#confirmdeleteitem').dialog('open');
});
在这最后一步中,我希望能够向对话框传递一些信息 - 比如点击了哪个删除按钮。但是我没有找到方法去做到这一点。
我可以事先将一个隐藏的对话框
div.dialog
插入到每个项目行中,或者在特定行的按钮被点击后插入一个对话框。然后$(this).parent()
的引用将抓取正确的行...有更简单的方法吗?