event.preventDefault()在Mozilla或IE中无效

编程入门 行业动态 更新时间:2024-10-27 10:27:37
本文介绍了event.preventDefault()在Mozilla或IE中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我终于在其他浏览器中测试我的网站(主要在Chrome中构建)。不幸的是,很多东西似乎功能不同。我将从第一个问题开始:登录后,我有一个JS / jQuery检查以确保用户名和密码匹配,失败时它应该停止提交。

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 very first problem: upon login, I have a JS/jQuery check to make sure the username and password match, and upon failure it should stop the submission.

然而,虽然它可以在Chrome和Safari中使用,但在Mozilla和IE中,提交仍在进行中(打到道歉页面,但仍然是我根本看不到的东西)。

However, while it works in Chrome and Safari, in Mozilla and IE the submission is still going through (hitting an apology page, but still something I'd rather just not see at all).

我已经尝试为 e.preventDefault()或 evt.preventDefault()但是它们都没有工作,表单仍然提交(对于后两个,它也使它在Chrome中提交)。这是我的代码,会喜欢任何想法:

I've tried subbing out event.preventDefault() for e.preventDefault() or evt.preventDefault() but none of them work, the form still submits (and for the second two it makes it so that it submits in Chrome as well). Here is my code, would love any thoughts:

function checkLogin() { // get the variables, execute some other checks (eg, things not blank) // run ajax code to determine if pieces match $.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"); e.preventDefault(); return false; } else { $('#loginoff').html(""); return true; } } }); }

请注意,该功能肯定会通过,并在用户名和时返回1密码不匹配,因为在所有情况下都会出现无效的用户名/密码消息。

Please note that the function is definitely going through and returning 1 when the username and password don't match as, in all cases, the 'Invalid username/password' message is coming up.

此外,如果有人对HTML感兴趣:

Also, in case anybody is interested in the HTML:

<form action="login.php" method="post" onsubmit="return checkLogin()"> <!-- code for inputs, eg username --> <input type="submit" class="btn" value="Log In"/> </form>

推荐答案

我建议阻止事件开始,然后,当您希望表单提交时,使用本机提交方法,因为它绕过jQuery。

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. } } }); });

这也允许你删除 async:false 这总是一件好事(除了网络工作者之外)。

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

更多推荐

event.preventDefault()在Mozilla或IE中无效

本文发布于:2023-08-02 16:34:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:event   preventDefault   Mozilla

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!