This question already has an answer here:
My current code is as follows:
<div class="fr_search">
<form action="/" accept-charset="UTF-8" method="post" id="search-theme-form">
.......
</form>
</div>
Now I want to write a function to change the form action and method, when a condition is met. How do I write this code?
For example,
function test() {
if (selectedIndex === 1)....
} // How do I write this code?
and your form need to have
name
is this caseI wanted to use Javascript change a form's action, so I could have different submit inputs within the same form linking to different pages. I also had the added complication of using Apache rewrite to change
example.com/page-name
intoexample.com/index.pl?page=page-name
. I found that changing the form's action causedexample.com/index.pl
(with no page parameter) to be rendered, even though the expected URL (example.com/page-name
) was displayed in the address bar. To get around this, I used Javascript to insert a hidden field to set the page parameter. I still changed the form's action, just so the address bar displayed the correct URL.In the form:
Here are my Apache rewrite rules:
I'd be interested in any explaination as to why just setting the action didn't work.
Try this:
If you're using jQuery, it's as simple as this: