开发者

jQuery live submit caugth in infinite loop

I'm trying to submit a form using jQuery and it worked just fine until I had to add a confirmation window so users can review their data before submission, here's the code:

$("#create-group-form").live('submit', function(e){
    e.preventDefault();
    var form = $(this);
    jConfirm('Here I display the group info...', 'Confirm Group', function(r){
        if ( r ) {
            form.submit();
        }
    });
});

I'm using the jAlert plugin for jQuery but it works just as a regular Confirm prompt with different styling, the pre开发者_如何学Pythonblem is that when users click Ok on the prompt it goes again into the live submit getting stuck in an infinite loop.

Is there a way to stop it from going in again this event after I confirm? I think I can unbind it somehow but I haven't found a way to do it successfully.

BTW I'm using live submit because the form is in a modal window.

Thanks in advance!


Call the form element's submit method, rather than the jQuery selection's one. This means the jQuery handler won't be triggered:

$("#create-group-form").live('submit', function(e){
    e.preventDefault();
    var form = this; // <-- this line changed
    jConfirm('Here I display the group info...', 'Confirm Group', function(r){
        if ( r ) {
            form.submit();
        }
    });
});


This will unbind your event handler before you call submit() on it again.

$("#create-group-form").live('submit', function(e){
    e.preventDefault();
    var form = $(this);
    jConfirm('Here I display the group info...', 'Confirm Group', function(r){
        if ( r ) {
            form.unbind('submit');
            form.submit();
        }
    });
});


Replace the submit button on the form with a button tag with an ID like #psuedo-submit. When #psuedo-submit is clicked, pop the modal window up and do an actual submit on confirmation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜