PreventDefault not working in IE

I am trying to submit a form using ajax, the form itself is being loaded via ajax event

Does the following jQuery code work in FF? chrome. In IE, 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?

+3
source share
1 answer

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

$("#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 this is not a problem is because it is not a browser-specific event object that you are dealing with, it is a jQuery event object that has normalized behavior.

+2
source

Source: https://habr.com/ru/post/1768789/


All Articles