jQuery live submit caugth in infinite loop

不想你离开。 提交于 2019-12-12 04:48:33

问题


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 preblem 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!


回答1:


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();
        }
    });
});



回答2:


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();
        }
    });
});



回答3:


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.



来源:https://stackoverflow.com/questions/5843277/jquery-live-submit-caugth-in-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!