event.preventDefault() not working in Mozilla or IE

前端 未结 5 2129
陌清茗
陌清茗 2021-01-23 04:31

I\'m finally at the point of testing my site in other browsers (built it mostly in Chrome). Unfortunately, a lot of stuff seems to function differently. I\'ll begin with the v

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 05:07

    I'd suggest preventing the event to begin with, then when you want the form to submit, do it with the native submit method since it bypasses jQuery.

    var form = document.getElementById("formid");
    $(form).submit(function(e){
        e.preventDefault();
    
        $.ajax({
            type: "POST",
            url: "check_login.php",
            data: {
                'username': username,
                'password': password
            },
            //async: false,
            success: function (result) {
                if (result == 1) {
                    $('#loginoff').html("Invalid username/password");
                } else {
                    $('#loginoff').html("");
                    form.submit(); // note, form is a form NODE, not a jQuery object containing a form.
                }
            }
        });
    
    });
    

    this also allows you to remove async: false which is ALWAYS a good thing(other than within webworkers.)

提交回复
热议问题