tags:

views:

21

answers:

2

Hello

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

+2  A: 

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>
Darin Dimitrov
It doesnt submit for me, is it because I have multiple links with the id="delete"? I list all users on same page
molgan
Yes, you cannot have multiple elements with the same id. Use class selectors instead: `<a class="delete">` and `$('.delete')`.
Darin Dimitrov
I updated my post with info, still nothing happens onclick :(
molgan
What does `FireBug` say? Is any ajax request sent to the server at all? Did you reference the jQuery library?
Darin Dimitrov
+1  A: 

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;"
womp
it shows the confirm-box but doesnt post anywhere, what might be wrong?
molgan