jquery .submit live click runs more than once

Posted by fxuser on Stack Overflow See other posts from Stack Overflow or by fxuser
Published on 2012-08-25T21:32:05Z Indexed on 2012/09/01 21:38 UTC
Read the original article Hit count: 325

Filed under:
|
|
|
|

I use the following code to run my form ajax requests but when i use the live selector on a button i can see the ajax response fire 1 time, then if i re-try it 2 times, 3 times, 4 times and so on...

I use .live because i also have a feature to add a post and that appears instantly so the user can remove it without refreshing the page...

Then this leads to the above problem... using .click could solve this but it's not the ideal solution i'm looking for...

jQuery.fn.postAjax = function(success_callback, show_confirm) {
    this.submit(function(e) {
        e.preventDefault();
        if (show_confirm == true) {
            if (confirm('Are you sure you want to delete this item? You can\'t undo this.')) {
                $.post(this.action, $(this).serialize(), $.proxy(success_callback, this));
            }
        } else {
            $.post(this.action, $(this).serialize(), $.proxy(success_callback, this));
        }
        return false;
    })
    return this;
};
$(document).ready(function() {
    $(".delete_button").live('click', function() {
        $(this).parent().postAjax(function(data) {
            if (data.error == true) {

            } else {

            }
        }, true);
    });
});?

EDIT: temporary solution is to change

this.submit(function(e) {

to

this.unbind('submit').bind('submit',function(e) {

the problem is how can i protect it for real because people who know how to use Firebug or the same tool on other browsers can easily alter my Javascript code and re-create the problem

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX