Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JavaScript to change the form action [duplicate]

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? 
like image 213
enjoylife Avatar asked Mar 19 '11 11:03

enjoylife


People also ask

Can a form action be a JavaScript function?

In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.

How do you duplicate a form in HTML?

Call the . clone() method on the selector which you want to copy and insert the newly created element in your existing HTML layout using append, pretend, insertAfter, etc.

How do you put multiple actions in a form?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.


1 Answers

function chgAction( action_name ) {     if( action_name=="aaa" ) {         document.search-theme-form.action = "/AAA";     }     else if( action_name=="bbb" ) {         document.search-theme-form.action = "/BBB";     }     else if( action_name=="ccc" ) {         document.search-theme-form.action = "/CCC";     } } 

And your form needs to have name in this case:

<form action="/"  accept-charset="UTF-8" method="post" name="search-theme-form" id="search-theme-form"> 
like image 195
bensiu Avatar answered Sep 23 '22 21:09

bensiu