简体   繁体   English

如何在IE8中取消提交?

[英]How to cancel submit in IE8?

I've been struggling to make this work for quite some time. 我一直在努力使其工作一段时间。 I have the following code: 我有以下代码:

$(document).ready(function (event) {
    $('form').submit(function (event) {
    ...
    });
});

I've found in most of the places, including on the jQuery site, that this will prevent the form from being submitted: 我在包括jQuery网站在内的大多数地方都发现,这将阻止表单的提交:

return false;

It doesn't. 没有。

Next I've read all related question and answers on this site and tried them all out. 接下来,我已经阅读了本网站上的所有相关问题和解答,并尝试了全部。 In Chrome and Firefox this works: 在Chrome和Firefox中,此方法有效:

event.preventDefault();

In IE8 it doesn't. 在IE8中没有。 I've read that IE doesn't support this and we should use this: 我读过IE不支持此功能,我们应该使用以下功能:

event.returnValue = false;

Although several people confirm that it worked for them (eg event.preventDefault() function not working in IE. Any help? ), it doesn't for me. 尽管有几个人确认它对他们有用 (例如event.preventDefault()函数在IE中不起作用。有什么帮助吗? ),但对我而言却不行。 I'm not sure why though. 我不确定为什么。 I have a form with a submit button, nothing special, but, even if I put only this piece of code, the form is still submitted: 我有一个带有“提交”按钮的表单,没什么特别的,但是,即使我只放了这段代码,表单仍然被提交:

$(document).ready(function (event) {
    $('form').submit(function (event) {
        event.returnValue = false;
    });
});

Any ideas? 有任何想法吗?

One last thing: I've read that we should test this first before calling preventDefault(): 最后一件事:我读到我们应该在调用preventDefault()之前先进行测试:

if (event.preventDefault) { event.preventDefault(); }

But in IE8, event.preventDefault exists and no error is thrown. 但是在IE8中,event.preventDefault存在,并且不会引发任何错误。 How can that be if it's unsupported by IE? 如果IE不支持怎么办?

Update 1: I've tried a simplified version with the following code: 更新1:我尝试使用以下代码进行简化:

CancelSubmitTest.aspx: CancelSubmitTest.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CancelSubmitTest.aspx.cs" Inherits="HtmlEditor.CancelSubmitTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="Scripts/CancelSubmit.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="SaveButton" runat="server"
            Text="Save" OnClick="SaveButtonClick" CausesValidation="true" ValidationGroup="SomeValidationGroup" />
    </div>
    </form>
</body>
</html>

CancelSubmitTest.aspx.cs CancelSubmitTest.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HtmlEditor
{
    public partial class CancelSubmitTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButtonClick(object sender, EventArgs e)
        {

        }
    }
}

CancelSubmit.js 取消提交.js

$(document).ready(function (event) {
    $('form').submit(function (event) {
        event.returnValue = false;
    });
});

It's a pretty straight-forward example. 这是一个非常简单的例子。 The JavaScript code is executed but the form is still submitted, although I set event.returnValue to false. 尽管我将event.returnValue设置为false,但已执行JavaScript代码,但仍提交表单。 What am I doing wrong? 我究竟做错了什么?

Update 2: I've changed the javascript code in the simplified project as follows: 更新2:我更改了简化项目中的javascript代码,如下所示:

$(document).ready(function (event) {
    $('form').submit(function (event) {
        return false;
    });
});

It works in IE but I still have to figure out why it doesn't work in my application. 它可以在IE中运行,但是我仍然必须弄清楚为什么它在我的应用程序中不起作用。 Thanks to everyone for your help. 感谢大家的帮助。

return false is equal to event.returnValue return false等于event.returnValue

But sometimes this does not work because of DOM compatibility 但有时由于DOM兼容性而无法正常工作

For Example: 例如:

// Not Working
OnClientClick="return confirm('Are you sure want to Delete this file?');"

// Working
OnClientClick="event.returnValue=confirm('Are you sure want to Delete this file?');"
  1. It does work: http://jsfiddle.net/AEC2U/ 它确实起作用: http : //jsfiddle.net/AEC2U/
  2. The problem is somewhere else in your code. 问题出在代码的其他地方。
  3. Post more code/stripped down example where it doesn't work. 发布更多无效的代码/示例。
$(document).ready(function () {
    $('form').submit(function () {
        alert( 'test' )        
        return false;
    });
});

This works for me no problem in IE8. 这对我来说在IE8中没有问题。 If anything all I did was get rid of the two passed event variables 如果我做的所有事情都摆脱了两个传递的事件变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM