输入名称为“提交”时如何使用Javascript发送表单? [英] How send a form with Javascript when input name is "submit"?

查看:90
本文介绍了输入名称为“提交”时如何使用Javascript发送表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如果一个表单输入的名称提交


,如何使用Javascript发送表单

背景:我将用户重定向到另一个带有隐藏HTML表单的页面。我无法更改(隐藏)输入的名称,因为其他页面位于另一台服务器上,输入需要完全按照原样。我的HTML表单看起来像这样:
$ b

< form id =redirectFormmethod =post action =http://www.example.com/>
< input name =searchtype =hiddenvalue =搜索此/>
< input name =submittype =hiddenvalue =立即搜索/>
< / form>

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

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

然而,由于一个输入的名称是submit(它不能是别的东西,或者其他服务器将不处理请求), document.getElementById('redirectForm')。submit 引用输入,因为它覆盖了表单函数 submit ()



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

值得注意的是:只需将输入名称更改为submit以外的内容即可。



您需要获取提交函数从不同的形式:

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

如果您已经有另一个< form> 标记,您可以使用它而不是创建另一个。


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.

解决方案

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.

这篇关于输入名称为“提交”时如何使用Javascript发送表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆