Javascript 未捕获类型错误:Chrome或SCRIPT65535中的非法调用:提交表单时IE中的调用对象无效

Javascript 未捕获类型错误:Chrome或SCRIPT65535中的非法调用:提交表单时IE中的调用对象无效,javascript,forms,internet-explorer,google-chrome,Javascript,Forms,Internet Explorer,Google Chrome,我得到了未捕获的TypeError:Chrome或SCRIPT65535中的非法调用:提交表单时IE中的调用对象无效 以下标记复制错误: <!DOCTYPE html> <html> <body> <p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p> <form id="myForm

我得到了未捕获的TypeError:Chrome或SCRIPT65535中的非法调用:提交表单时IE中的调用对象无效

以下标记复制错误:

<!DOCTYPE html>
<html>
<body>

<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="myForm" action="form_action.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
function myFunction()
{
var submit2 = myForm.submit;
submit2();
}
</script>

</body>
</html>
直接从myForm.submit()中读取数据,然后所有操作都不会出错


所以如何解决这个问题对我来说很清楚,对我来说很有趣,为什么“间接”表单提交会导致错误

当使用
myForm调用submit方法时,submit()知道上下文是myForm,但当我们使用
submit2()
调用它时,没有上下文(全局上下文),所以它给出了错误。

要修复此错误,必须通过将其内容设置为myForm来调用
submit2()
方法

示例代码是

var submit2 = myForm.submit;
submit2.call(myForm);
您可以使用call或apply更改上下文

有关呼叫和应用的更多信息:


  • 您仍在调用
    myForm.submit
    而不使用
    ()
    。奇怪的是,它在IE9-10中运行良好,但在IE11中却没有,这样的错误只出现在IE11和Chrome中。当它与上下文有问题时(这是最有可能的),那么它也应该在IE9-10中重现
    var submit2 = myForm.submit;
    submit2.call(myForm);