How to add a warning modal to delete
I have the following MySQL for a delete button.
DELETE FROM mytable
WHERE id = $id
I want to add a jquery modal to confirm to proceed. "Are you sure to delete? Yes | No"
If you click YES then it will execute to delete, if it is NO then exit the modal and go back to the page.
The id in each anchor is added dynamically.
echo anchor('admin/categories/delete/'.$list['id'],'delete',array('class' => 'modalInput'));
This output the following html.
HTML,
...
<tr valign='top'>
<td>1</td>
<td>Forsiden</td>
<td align='center'>active</td>
<td align='center'><a href="http://127.0.0.1/test/index.php/admin/categories/edit/1">edit</a> | <a href="http://127.0.0.1/test/index.php/admin/categories/delete/1">delete</a></td>
</tr>
<tr valign='top'>
<td>2</td>
<td>Front top</td>
<td align='center'>active</td>
<td align='center'><a href="http://127.0.0.1/test/index.php/admin/categories/edit/2">edit</a> | <a href="http://127.0.0.1/test/index.php/admin/categories/delete/开发者_运维知识库2">delete</a></td>
</tr>
...
What is best way to do this?
< script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to delete?")
if (answer){
$.post('/delete/post', {id: '345'});
}
}
//-->
< /script>
Something like this probably. You will have to pass the data where the '345' is...
One option would be to give the delete links a class, and then you can do something like this:
// simple solution
$('.delete_link').click(function() {
// if the user clicks "no", this will return false, stopping the link from being triggered
return confirm("Are you sure you want to delete?");
});
if you want the deletion to happen with ajax:
// ajax soluction
$('.delete_link').click(function() {
if (confirm("Are you sure you want to delete?")) {
$.post(this.href);
}
return false;
});
Also be sure to check out jqueryui's confirmation modals: http://jqueryui.com/demos/dialog/#modal-confirmation
精彩评论