jQuery event.preventDefault() 在 Firefox 中不起作用(包括 JSFiddle)
这是与这里其他一些类似的重复,但我认为在这种情况下我正确使用了 event.preventDefault()
。
这是一个 JSFiddle,您可以使用以下代码查看代码: http://jsfiddle.net/SeEw2/2/
基本上,单击“提交”按钮。
在 Chrome 中:没有任何反应 - 正确响应。
在 Firefox 中:页面重新加载,哦不!
那么为什么 Firefox 会重新加载页面,而 Chrome 却不会呢?我一直在对其进行 Firebug,并且没有出现任何错误......
This is a kind of similar duplicate to some others here, but I think I'm using event.preventDefault()
correctly in this case.
Here's a JSFiddle you can see the code with: http://jsfiddle.net/SeEw2/2/
Basically, click the Submit button.
In Chrome: Nothing happens - correct response.
In Firefox: Page reloads, oh noes!
So why is the page reloading in Firefox and not Chrome? I've been Firebugging it and no errors come up in either...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
啊,我过去也遇到过类似的问题。而不是 event.preventDefault() 尝试将事件传递给:
我知道它说 IE,但自从 =] 以来我从未遇到过问题
Ah I've had a similar problem in that past. Rather than event.preventDefault() try passing the event to:
I know it says IE, but I've never had a problem with it since =]
与其在提交按钮上寻找点击事件,为什么不使用 $(form).submit处理程序?
只要处理程序末尾有“return false”,页面就不会重新加载。
Instead of looking for a click event on the submit button, why not use the $(form).submit handler?
As long as you have 'return false' at the end of the handler, the page won't reload.
因为您没有像
function(event)
这样传递事件对象,但我的问题是,当您可以使type="button"
和 onclick 传递时,为什么还要经历所有这些价值观?本质上就是你在整个过程中所做的事情。Because your not passing the event object like
function(event)
, but my question is why even go through all this when you can make thetype="button"
and onclick pass the values? In essence that what your doing with this whole process.我用这种方式解决了这个问题,由于我的代码有点不同,这可能对其他人有帮助。我最初的代码如下所示。
我的 js 函数看起来必须
在函数调用中传递参数事件并在函数中接收它。
js代码:
希望有帮助。
I solved it this way, due my code is a little bit different this may be helpful for other people. My initial code looked like this.
My js function looked like
I had to pass the parameter event in the function call and receive it in the function.
js code:
I hope it helps.
代码中的变量
event
未初始化。http://jsfiddle.net/SeEw2/4/
摘录:
The variable
event
in your code is not initialized.http://jsfiddle.net/SeEw2/4/
extract :