RJK
RJK

Reputation: 151

jQuery AJAX only runs once

I've recently started jQuery and I have run into a little hiccup, when I my ajax function it only runs once, here is the code:

$('input').live('click', function(event){
     var conf = confirm("Are you sure you want to delete this user?");
     if(conf == true)
     {
         var list = document.getElementById("Users");
         document.getElementById("msg").innerHTML = "Deleting...";  

         $.ajax({
               type: "GET",
               url: "Pages/Functionality/delete-user.php",
               data: "userid=" + list.options[list.selectedIndex].value + "&id=" + getURLParameter('id'),
               success: function(m){
                   document.getElementById("userList").innerHTML = m                       
               },
               error: function (xhr, status, error) {
               if (xhr.status > 0) alert('Error Occured!\n\nError: ' + error + '\n' + 'Status: ' + status);
           }
         }); 
     }
});

When it is ran the second time it gets up to the ajax part and stops, I have tried .on but on the second time it doesn't run at all and I have tried .delegate, same thing happens as when using .live but it runs multiple times.

Again I have looked everywhere for a solution so I decided to try and get some help online.

Cheers,

Russell

EDIT: I have also tried setting the cache the false, but that didn't work.

Upvotes: 0

Views: 1045

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Try returning false from your handler at the end to prevent the default action:

$(document).on('click', 'input', function(event) {
    ...
    return false;
});

Upvotes: 1

Related Questions