最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Changing form action based on select value - with JavaScript - Stack Overflow

programmeradmin2浏览0评论

I have a form that I wish to change the action of, depending on what value a user selects in a drop down box.

The example code below shows each option value, I want to change the form action to whatever option value is chosen on a button press, how can I do this with JavaScript?

 <form name ="form1" action="VariableFormAction" method ="post" target="_blank">

<select name="formchoice">



<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>

</select>
<input type="submit" value="Go" class="button">
</form>

I have a form that I wish to change the action of, depending on what value a user selects in a drop down box.

The example code below shows each option value, I want to change the form action to whatever option value is chosen on a button press, how can I do this with JavaScript?

 <form name ="form1" action="VariableFormAction" method ="post" target="_blank">

<select name="formchoice">



<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>

</select>
<input type="submit" value="Go" class="button">
</form>
Share Improve this question asked Mar 11, 2018 at 15:01 SamoliverczSamolivercz 2201 gold badge4 silver badges11 bronze badges 1
  • 1 Possible duplicate of How to set form action through JavaScript? – Matt Healy Commented Mar 11, 2018 at 15:04
Add a ment  | 

3 Answers 3

Reset to default 2

In order to do this you need to specify an onSubmit JavaScript event handler on the form that retrieves the value of the select list and updates the form action.

You can do this using the following code.

//Add onSubmit() event handler to form to call JavaScript method when the form is submitted.
<form name ="form1" onSubmit="actionOnSubmit()" method ="post" target="_blank">

<select id="formchoice" name="formchoice">

<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>

</select>
<input type="submit" value="Go" class="button">
</form>


<script>

function actionOnSubmit()
{

//Get the select select list and store in a variable
var e = document.getElementById("formchoice");

//Get the selected value of the select list
var formaction = e.options[e.selectedIndex].value;

//Update the form action
document.form1.action = formaction;

}
</script>

Note, for this to work you will need to make sure the select list has an id as you are using the document.getElementById[] JavaScript method to retrieve the value of the control.

You could also call the JavaScript in the OnChange() event of the control. The issue with this is that it implies that the value has changed before the form is submitted. If the user simply left the default select list value it is possible that the OnChange() event would never fire.

You can try like following.

 function selectChanged(ctrl) {
    var val = ctrl.value;
    var frm = document.getElementById('form1');
    frm.action = val;
  }
<form id="form1" name="form1" action="/formaction1" method="post" target="_blank">
  <select name="formchoice" onchange="selectChanged(this)">
    <option value="/formaction1">function 1</option>
    <option value="/formaction2">function 2</option>
   <option value ="/formaction3">function 3</option>
 </select>
  <input type="submit" value="Go" class="button">
</form>

Use on change attribute.

<select name="forma" onchange="location = this.value;">
    <option value="Home.php">Home</option>
    <option value="Contact.php">Contact</option>
    <option value="Sitemap.php">Sitemap</option>
</select>
发布评论

评论列表(0)

  1. 暂无评论