javascript IE 11 无法提交 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20723776/
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-10-27 19:07:09  来源:igfitidea点击:

IE 11 cannot submit an HTML form

javascripthtmlformsinternet-explorerinternet-explorer-11

提问by slevin

I have this HTML form

我有这个 HTML 表单

<form name="nextform" action="anotherpage.php" method="post" enctype="multipart/form-data">
    <input name="pinid" id="pinid" type="hidden">
    <input type="submit" name="submit" id="post" value="Lets Go" class="formButtonMap">
</form>

piniddynamically gets a value using JavaScript. When it gets a value I alert it and it works.

pinid使用 JavaScript 动态获取值。当它获得一个值时,我会提醒它并且它可以工作。

But, when I click the Lets Gobutton, nothing happens. I see the Internet Explorer loading for a couple of minutes and then I get the message “The webpage does not respond”. If I hit refresh it goes to anotherpage.phpbut the values from the form did not arrive to the server.

但是,当我单击Lets Go按钮时,什么也没有发生。我看到 Internet Explorer 加载了几分钟,然后收到消息“网页没有响应”。如果我点击刷新,它会转到anotherpage.php但表单中的值没有到达服务器。

Rarely shows this message:

很少显示此消息:

Visual Studio Just-In-Time Debugger

An unhandled win32 exception occured in iexplorer.exe [688]

Possible Debuggers :

New Instance of Microsoft Visual Studio 2012

Visual Studio 即时调试器

iexplorer.exe 中发生未处理的 win32 异常 [688]

可能的调试器:

Microsoft Visual Studio 2012 的新实例

This behavior is observed only in Internet Explorer 11.0.2. The form works in older versions of Internet Explorer and also in Chrome and Firefox. I get no errors in IE's console.

此行为仅在 Internet Explorer 11.0.2 中观察到。该表单适用于旧版本的 Internet Explorer,也适用于 Chrome 和 Firefox。我在 IE 的控制台中没有收到任何错误。

Here is the JavaScript code, placed above the form:

这是放置在表单上方的 JavaScript 代码:

// called when another button is clicked - basicaly is websockets
function save() {
    var so = new WebSocket("ws://localhost:8000");
    so.onerror = function (evt) {
        alert('problem');
    }

    if (sara == 'LINES') {
        so.onopen = function() {
            so.send(JSON.stringify({
                command: 'insertAll',
                name: document.getElementById('name').value
            }));
        }
    }

    if (sara == 'POLY') {
        so.onopen = function() {
            so.send(JSON.stringify({
                command: 'insertHalf',
                name: document.getElementById('name').value
            }));
        }
    }

    so.onmessage = function (evt) {
        var received_msg = evt.data;

        document.getElementById("next").style.display = "block";
        document.getElementById("name").value = "";
        document.getElementById("descr").value = "";
        clearLinks();

        document.getElementById("pinid").value = received_msg;
        alert(document.getElementById("pinid").value); // works

        so.close();
    }
}

I tried to edit the code using document.getElementById("nextform").submit();, problem is still there.

我尝试使用 编辑代码document.getElementById("nextform").submit();,问题仍然存在。

Is it me? Is it a bug? What am I missing?

那是我吗?这是一个错误吗?我错过了什么?

采纳答案by Andy

I believe this is a bug when setting form values to empty in IE.

我相信这是在 IE 中将表单值设置为空时的错误。

I would suggest trying a different method to resetting the form values, I have used this method in the past:

我建议尝试不同的方法来重置表单值,我过去曾使用过这种方法:

document.getElementById('name').parentNode.innerHTML = '';

回答by RobG

Maybe not your issue, but:

也许不是你的问题,但是:

<input type="submit" name="submit" ... >

Giving a form control a name of submitwill replace the form's submitmethod with a reference to the control, so calling form.submit()will attempt to "call" the input.

给表单控件一个名称submit将使用对控件的引用替换表单的提交方法,因此调用form.submit()将尝试“调用”输入。

回答by jayesh

hi might be problem in your code you miss to add id in form and you try to access form by it's id that you not define.

嗨,您的代码可能有问题,您错过了在表单中添加 id 的问题,并且您尝试通过未定义的 id 访问表单。

document.getElementById("nextform").submit();

its required

它的要求

<form name="nextform" id="nextform" action="anotherpage.php" method="post" enctype="multipart/form-data">

...
...
...

</form>

回答by Ben Adams

Trace through what happens in the anotherpage.php page when it receives the postback, evt.data might not be encoded as you expect (is it binary or text, if text, is it utf-8).

跟踪 anotherpage.php 页面在收到回发时发生的情况,evt.data 可能没有按照您的预期进行编码(它是二进制还是文本,如果是文本,则是 utf-8)。

Postback to a different page where all it does is output the posted back values.

回发到不同的页面,它所做的只是输出回发的值。

Does the socket close throw an exception?

套接字关闭是否抛出异常?

 so.close();

回答by slevin

My original code has a form with more than 5 fields. When submitted calls the save(). save()function also clears the fields using JS.

我的原始代码有一个包含 5 个以上字段的表单。提交时调用save(). save()函数还使用 JS 清除字段。

There is a bug in IE11, that crashes the browser if you try to clear more than 5 fields , using JS. See here, there is workaround.

IE11 中存在一个错误,如果您尝试使用 JS 清除 5 个以上的字段,则会导致浏览器崩溃。看到这里,有解决方法。

That bug crashes the first form, then the nextformform and also the browser. I APOLOGISE for not posting all my code, I did not know it had to do with the first form.

该错误使第一个表单崩溃,然后是nextform表单以及浏览器。我为没有发布我所有的代码而道歉,我不知道这与第一种形式有关。

Because I thought the same piece of code had two different problems , I posted another question, very similar , here

因为我认为同一段代码有两个不同的问题,所以我发布了另一个问题,非常相似,在这里

回答by Luchezar

In my case the input button had the same ID and NAME. So you can check if they are the same and if indeed they are the same use different value for one of the parameters.

就我而言,输入按钮具有相同的 ID 和 NAME。因此,您可以检查它们是否相同,如果它们确实相同,则对其中一个参数使用不同的值。

I hope it helps.

我希望它有帮助。