简体   繁体   中英

Javascript/Jquery stops working after Ajax call

I have an ajax call that works great the first time the form is submitted after that all javascript on the page seems to break. As well as the form won't submit with ajax again.

Here is my ajax call:

$('form').submit(function(event) {
   $('input:submit').attr("disabled", true).after('<p class="loading">Searching...</p>');

$.ajax({
  type: "POST",
  url: pathname,
  data: $(this).serialize(),
  success: function(data) {
    $('#container').html("<div id='message'></div>");
    $('#message').append(data).hide().fadeIn(1500);
  },
});
event.preventDefault();
});

I'm getting no errors in my console. Any ideas what might be causing this?

I solved the issue, the main page content was changing. Which was causing the javascript to unload. So I need to use Jquery .on/.live and then it works. For what ever reason this worked fine on one server and not on another.

Remove disabled after submitting the form. And remove the , mentioned by @JustinRusso.

$('form').submit(function(event) {
   $('input:submit').attr("disabled", true).after('<p class="loading">Searching...</p>');

    $.ajax({
      type: "POST",
      url: pathname,
      data: $(this).serialize(),
      success: function(data) {
        $('#container').html("<div id='message'></div>");
        $('#message').append(data).hide().fadeIn(1500);
        $('input:submit').removeAttr("disabled");
      }
    });
    event.preventDefault();
});

Use as below: No Need to define event.preventDefault();

$('form').submit(function(event) {
   $('input:submit').attr("disabled", true).after('<p class="loading">Searching...</p>');

    $.ajax({
      type: "POST",
      url: pathname,
      data: $(this).serialize(),
      success: function(data) {
        $('#container').html("<div id='message'></div>");
        $('#message').append(data).hide().fadeIn(1500);
        $('input:submit').removeAttr("disabled");
      },
    });
   return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM