tags:

views:

34

answers:

1

Hi, I am trying to submit a form with ajax, the form itself is loaded via a ajax event

The following Jquery code works in FF?chrome. In IE, the form submission is not prevented

$("#admin_main").delegate("#create_user_form", "submit", function (event) {
    if (event.preventDefault) {
        event.preventDefault();
    } else {
        event.returnValue = false;
    }
    $.post('create_user', $("#create_user_form").serialize(), function (data) {
        $("#admin_main").html(data);
    }, "html");
});

Any workarounds?

+1  A: 

jQuery normalizes this, you can reliably call .preventDefault(), like this:

$("#admin_main").delegate("#create_user_form", "submit", function (event) {
  event.preventDefault();
  $.post('create_user', $("#create_user_form").serialize(), function (data) {
    $("#admin_main").html(data);
  }, "html");
});

Or if you want to completely kill the event, return false:

$("#admin_main").delegate("#create_user_form", "submit", function () {
  $.post('create_user', $("#create_user_form").serialize(), function (data) {
    $("#admin_main").html(data);
  }, "html");
  return false;
});

The reason it's not an issue to call it is because it's not a browser-specific event object that you're dealing with, it's a jQuery event object that has normalized behavior.

Nick Craver
Hi I already tried that, doesn't work. Above code is supposed to work
RisingSun
@RisingSun - You have another issue then, it's not the above code...for example invalid markup, or any JS error before this will bomb out and normal browser behavior takes over....are you not seeing an error in the status bar?
Nick Craver
Well, even return false; doesn't prevent form submission.
RisingSun
Well. the console shows both events occuring in rapid succession
RisingSun
@RisingSun - And you're getting no errors at all? Which version of IE?
Nick Craver
You got it! IE9 was doing some compatiblity mischief. Will immediately uninstall it. Thnx
RisingSun
@RisingSun - Ah, that makes more sense :) If it helps, IE9 has *many* JavaScript/event issues still, it needs a bit of work :)
Nick Craver