Javascript jQuery event.preventDefault() 在 Firefox 中不起作用(包括 JSFiddle)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4585970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:05:38  来源:igfitidea点击:

jQuery event.preventDefault() not working in Firefox (JSFiddle included)

javascriptjqueryfirefoxgoogle-chromejsfiddle

提问by Hyman

This is a kind of similar duplicate to some others here, but I think I'm using event.preventDefault()correctly in this case.

这是一种与此处其他人类似的重复,但我认为event.preventDefault()在这种情况下我使用正确。

Here's a JSFiddle you can see the code with: http://jsfiddle.net/SeEw2/2/

这是一个 JSFiddle,您可以使用以下代码查看代码:http: //jsfiddle.net/SeEw2/2/

Basically, click the Submit button.

基本上,单击提交按钮。

In Chrome:Nothing happens - correct response.

在 Chrome 中:没有任何反应 - 正确响应。

In Firefox: Page reloads, oh noes!

在 Firefox 中:页面重新加载,哦不!

So why is the page reloading in Firefox and not Chrome? I've been Firebugging it and no errors come up in either...

那么为什么页面会在 Firefox 而不是 Chrome 中重新加载?我一直在调试它,但两者都没有出现错误......

回答by Shikiryu

The variable eventin your code is not initialized.

event代码中的变量未初始化。

http://jsfiddle.net/SeEw2/4/

http://jsfiddle.net/SeEw2/4/

extract :

提炼 :

 $('#ajaxsearch').click(function(event) {

        // Stop the Search input reloading the page by preventing its default action
        event.preventDefault();

回答by Dartoxian

Ah I've had a similar problem in that past. Rather than event.preventDefault() try passing the event to:

啊,我过去也遇到过类似的问题。而不是 event.preventDefault() 尝试将事件传递给:

    function ie8SafePreventEvent(e){
    if(e.preventDefault){ e.preventDefault()}
    else{e.stop()};

    e.returnValue = false;
    e.stopPropagation();        
}

I know it says IE, but I've never had a problem with it since =]

我知道它说 IE,但自从 =] 以来我从来没有遇到过问题

回答by CamelBlues

Instead of looking for a click event on the submit button, why not use the $(form).submithandler?

与其在提交按钮上寻找点击事件,不如使用$(form).submit处理程序?

As long as you have 'return false' at the end of the handler, the page won't reload.

只要在处理程序的末尾有“return false”,页面就不会重新加载。

回答by Babiker

Because your not passing the event object like function(event), but my question is why even go through all this when you can make the type="button"and onclick pass the values? In essence that what your doing with this whole process.

因为您没有像 那样传递事件对象function(event),但我的问题是,当您可以使type="button"和 onclick 传递值时,为什么还要经历这一切?从本质上讲,你在整个过程中做了什么。

回答by Andres Ramos

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.

我是这样解决的,由于我的代码有点不同,这可能对其他人有帮助。我最初的代码是这样的。

<form id="enrollstudentform" onsubmit="return enrollStudents()">
    ...
</form>

My js function looked like

我的 js 函数看起来像

function enrollStudents() {
    // Stop Form.
    event.preventDefault();
    // Other code
}

I had to pass the parameter event in the function call and receive it in the function.

我必须在函数调用中传递参数事件并在函数中接收它

<form id="enrollstudentform" onsubmit="return enrollStudents(event)">
    ...
</form>

js code:

js代码:

function enrollStudents(event) {
    // Stop Form.
    event.preventDefault();
    // Other code
}

I hope it helps.

我希望它有帮助。