event.preventDefault() not working in Mozilla or IE

前端 未结 5 2131
陌清茗
陌清茗 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:27

    I eventually figured out how to make it work in general:

    in the section, the function should be written as:

    Note the 'event' as an input for checkLogin

    Then the function I used is below. Key changes are a.) preventing the default submission at the start (note, $.Event(e) is necessary or else it won't work in IE), b.) defining the form using getElementById, c.) submitting the function using form.submit(). Hopefully people find this helpful.

    function checkLogin(e)
    {
    var form = document.getElementById("mainloginform");
    $.Event(e).preventDefault();
    
    // set error counter
    var counter = 0;
    var username = $('#loginusername').val();
    var password = $('#loginpassword').val();
    
    // check that username is not blank
     if (username==null || username=="")
    {
        $('#usernameoff').html("Must enter your username");
        counter++;
    }
    else
    {
        $('#usernameoff').html("");
    }
    
    // check that password is not blank
     if (password==null || password=="")
    {
        $('#passwordoff').html("Must enter your password");
        counter++;
    }
    else
    {
        $('#passwordoff').html("");
    }
    
    // if counter zero at this point, return false, else return true
    if (counter > 0)
    {
        return false;
    }
    else
    {
        $.post("check_login.php", {'username': username, 'password': password}, function(result)
        {
            if(result == 1)
            {
                $('#loginoff').html("Invalid username/password");
                return false;
            }
            else
            {
                $('#loginoff').html("");
                form.submit();
                return true;
            }
        });
    }
    }
    

提交回复
热议问题