当输入名称为“提交”时,如何使用 Javascript 发送表单?

发布于 2024-12-24 17:15:58 字数 906 浏览 1 评论 0原文

问题:如果一个表单输入的名称为 submit,如何使用 Javascript 发送表单?

背景:我将用户重定向到另一个带有隐藏 HTML 表单的页面。我无法更改(隐藏)输入上的名称,因为另一个页面位于另一台服务器上,并且输入需要完全相同。我的 HTML 表单如下所示:

<form id="redirectForm" method="post" action="http://www.example.com/">
  <input name="search" type="hidden" value="search for this" />
  <input name="submit" type="hidden" value="search now" />
</form>

我今天使用以下 javascript 行自动发送表单:

document.getElementById('redirectForm').submit();

但是,由于一个输入的名称是“submit”(它不能是其他名称,否则其他服务器将无法处理该请求) , document.getElementById('redirectForm').submit 引用输入,因为它会覆盖表单函数 submit()

Firefox 中的错误消息为:错误:document.getElementById("requestform").submit 不是函数。 Safari 中出现类似错误消息。

Question: How can you send a form with Javascript if one form input has the name submit?

Background: I am redirecting the user to another page with a hidden HTML form. I cannot change name on the (hidden) inputs, since the other page is on another server and the inputs need to be exactly as they are. My HTML form looks like this:

<form id="redirectForm" method="post" action="http://www.example.com/">
  <input name="search" type="hidden" value="search for this" />
  <input name="submit" type="hidden" value="search now" />
</form>

I use the following javascript line to send the form automatically today:

document.getElementById('redirectForm').submit();

However, since the name of one input is "submit" (it cannot be something else, or the other server won't handle the request), document.getElementById('redirectForm').submit refers to the input as it overrides the form function submit().

The error message in Firefox is: Error: document.getElementById("requestform").submit is not a function. Similar error message in Safari.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

演多会厌 2024-12-31 17:15:58

值得注意:将输入名称更改为“提交”以外的名称通常要容易得多。仅当确实不可能时,才请使用下面的解决方案。

您需要从不同的表单获取 submit 函数:

document.createElement('form').submit.call(document.getElementById('redirectForm'));

如果您已有另一个

标签,您可以使用它而不用创建另一个标签。

Worth noting: It's often a lot easier to just change the input name to something other than "submit". Please use the solution below only if that's really not possible.

You need to get the submit function from a different form:

document.createElement('form').submit.call(document.getElementById('redirectForm'));

If you have already another <form> tag, you can use it instead of creating another one.

我的痛♀有谁懂 2024-12-31 17:15:58

使用 submit() HTMLFormElement.prototype 中的方法:

HTMLFormElement.prototype.submit.call(document.getElementById('redirectForm'));

Use submit() method from HTMLFormElement.prototype:

HTMLFormElement.prototype.submit.call(document.getElementById('redirectForm'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文