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

发布于 2024-10-10 18:27:31 字数 403 浏览 8 评论 0原文

这是与这里其他一些类似的重复,但我认为在这种情况下我正确使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我是有多爱你 2024-10-17 18:27:32

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

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

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

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

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

    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 =]

心意如水 2024-10-17 18:27:32

与其在提交按钮上寻找点击事件,为什么不使用 $(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.

末が日狂欢 2024-10-17 18:27:32

因为您没有像 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 the type="button" and onclick pass the values? In essence that what your doing with this whole process.

飘落散花 2024-10-17 18:27:32

我用这种方式解决了这个问题,由于我的代码有点不同,这可能对其他人有帮助。我最初的代码如下所示。

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

我的 js 函数看起来必须

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

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

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

js代码:

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

希望有帮助。

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

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:

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

I hope it helps.

清风疏影 2024-10-17 18:27:31

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

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

摘录:

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

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

The variable event in your code is not initialized.

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

extract :

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

        // Stop the Search input reloading the page by preventing its default action
        event.preventDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文