Javascript stops working after ajax submit
I've got a page that loads a php file inside a div using load(). Inside the ajax-loaded page there's a button which will load a second php file in the same div. This works fine and on the second div a form will be loaded. Inside this form I've got an info button (after the e-mail address field). Clicking this button will open a dialog, using the UI dialog. So far so good, everything works fine. Even after clicking back in the form page and clicking the button on the index page.
The problem appears after submitting the form. This will trigger a javascript function which will post the form using $.ajax(). The response json data will be handled. If an error occured, some error messages will be set without reloading the form. If the form is ok, the user will be returned to the index page, using the load() function.
The problem is that (after clicking on the button on the index to load the form again) the info button isn't working anymore. Can someone tell me what I'm doing wrong here?
Form page code:
- $(function(){
- $("#email-info").dialog({
- autoOpen: false,
- width:400,
- modal:true
- });
- $('#popup-trigger').click(function(e){
- e.preventDefault();
- $('#email-info').dialog('open');
- });
- $('#edit').ajaxform({
// options
});
- });
- <form action="filename.php" method="post" name="edit" id="edit" enctype="multipart/form-data">
- <!-- fields come here -->
- </form>
- <div id="email-info" title="Info">
- <p>Popup content here.</p>
- </div>
The ajaxform function is defined in a .js file which handle form data. It's too much code to post here, but the ajax function looks like this:
- ;(function($) {
- $.fn.ajaxform = function(options) {
- var form = $(this);
- var container = $(options.container);
- form.submit(function(){
- form.css('opacity',0.5);
- $(":submit").attr('disabled', 'disabled').addClass('disabled');
- container.addClass(options.loaderClass);
- $('.' + options.error.class_name + ':not(#' + options.message_id + ' .' + options.error.class_name + ')').removeClass(options.error.class_name);
- $('.' + options.error.message_class_name).remove();
- $.ajax({
- type:'POST',
- url:form.attr('action'),
- data:$(this).serializeArray(),
- success:afterValidate,
- dataType:"json",
- complete:resizeContainer
- });
- return false;
- });
- }
- });