简体   繁体   English

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

[英]Uncaught TypeError: Illegal invocation in Chrome or SCRIPT65535: Invalid calling object in IE when submit form

I am getting Uncaught TypeError: Illegal invocation in Chrome or SCRIPT65535: Invalid calling object in IE when submit form. 我收到Uncaught TypeError:Chrome或SCRIPT65535中的非法调用:提交表单时IE中的调用对象无效。

Following markup reproduces error: 以下标记重现错误:

<!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>

But when instead lines: 但当相反的行:

var submit2 = myForm.submit;
submit2();

Directly sumbit form myForm.submit() then all work without error. 直接汇总形式myForm.submit()然后所有工作没有错误。

So how to fix problem is clear for me, for me is interesting why "indirect" form submit causes errors 所以如何解决问题对我来说很明显,对我而言,“间接”表单提交导致错误的原因很有趣

The submit method when called with myForm.submit() knows the context is myForm, but when we are calling it using submit2() , there is not context ( global context), so its giving error. 使用myForm.submit()调用时,submit方法知道上下文是myForm,但是当我们使用submit2()调用它时,没有上下文(全局上下文),因此它给出了错误。

To fix the error, you have to call the submit2() method by setting the content of this to the myForm. 要修复错误,您必须通过将此内容设置为myForm来调用submit2()方法。

The sample code is 示例代码是

var submit2 = myForm.submit;
submit2.call(myForm);

You can either use call or apply for changing the context. 您可以使用呼叫或申请更改上下文。

More Info on call and apply: 更多信息随叫随到:

  1. apply mdn reference 应用mdn参考
  2. call mdn reference 调用mdn参考

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

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