Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Confirm messagebox when delete, $.post

I have this atm:

<a href="<%= Url.RouteUrl("UserDelete", new { userID = item.UserID }) %>" onclick="$.post(this.href); return false;"><img src="/Content/Admin/Images/Icons/delete.gif" /></a>

How can I modify this one so it also shows a confirm messagebox?

As it is now nothing happens when I click it, it doesnt post, what more is needed for it to work?

EDIT:

    <form action="/Admin/Users" method="post">

   <script type="text/javascript">
       $(function() {
           $('.delete').click(function() {
               $.post(this.href, {}, function(data) {
               });
               return false;
           });
       }); 
</script>

And each link:

<a class="delete" href="/Admin/User/Delete/71"><img src="/Content/Admin/Images/Icons/delete.gif" /></a>

no luck, nothing happens "onclick", not even a javascript error

/M

Upvotes: 0

Views: 842

Answers (2)

womp
womp

Reputation: 116987

Are you sure nothing is happening? Just calling $.post(url) will request the page, but it ignores any result. You may want to review the docs for the $.post function to accomplish what you'd like to do?

You can add a confirm box like this:

onclick = "if (confirm('Are you sure?')) $.post(this.href); return false;"

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

Indeed more is needed. I would advice you to separate javascript from HTML:

<a id="delete" href="<%= Url.RouteUrl("UserDelete", new { userID = item.UserID }) %>">
    <img src="/Content/Admin/Images/Icons/delete.gif" />
</a>

and then register a click handler to this anchor in javascript:

<script type="text/javascript">
$(function() {
    $('#delete').click(function() {
        $.post(this.href, {}, function(data) {
            alert('user successfully deleted');
        });
        return false;
    });
});
</script>

Upvotes: 2

Related Questions