Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

html - How send a form with Javascript when input name is "submit"?

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...