简体   繁体   中英

How can i change Form value Action based on dropdown value selected

I have two forms the first form contains two drop-down element. I want the second form action value to be changed based on the drop-down values selected in form 1

Form 1

<form method="post" style="padding: 10px 0px 10px 5px;">
<label>Front/Rear Tire:<br/><select name="tire" class="first">
<option  value="">Select</option>
  <option  value="rear">Rear Tire</option>
  <option  value="front">Front Tire</option>

</select> </label>
  <label>Tractor Type:<br/><select name="tractor" class="second" disabled>
    <option value="">Select Type</option>
    <option value="2wd">2WD</option>
    <option value="mfwd">MFWD</option>
    <option value="4wd">4WD</option>

  </select>  </label>


</form>

Form 2 looks like

<form method="post" action="A.php" >
</form>

I want.

  1. if rear Tire and (2WD or 4wd) Selected i want to change action value to 'A.php`

  2. if Rear Tire and (MFWD) Selected i want to change action value to 'B.php`

  3. if Front Tire and (2WD or $wd) Selected i want to change action value to 'C.php`

  4. if front Tire and (MFWD) Selected i want to change action value to 'D.php`

I have used data-attributes for storing the file names. You need to make sure to keep names of attributes in first select same as values in second select.

<option  value="rear" data-2wd="A.php" data-4wd="A.php" data-mfwd="B.php" >Rear Tire</option>
<option  value="front" data-2wd="C.php" data-4wd="C.php" data-mfwd="D.php" >Front Tire</option>

Use change event with delegate on to fetch the change in select menu.

$("#one select").on("change",function(){
  var sel1 = $("select.second").find(":selected").val();
  var sel2 = $("#one select.first").find(":selected").attr("data-"+sel1);
  $("#two").attr("action",sel2);
})

Please refer this fiddle .

it's possible to do with small jQuery code

<form method="post" style="padding: 10px 0px 10px 5px;">
<label>Front/Rear Tire:<br/><select name="tire" class="first">
<option  value="">Select</option>
  <option  value="rear">Rear Tire</option>
  <option  value="front">Front Tire</option>

</select id="drop"> </label>
  <label>Tractor Type:<br/><select name="tractor" class="second" disabled>
    <option value="">Select Type</option>
    <option value="2wd">2WD</option>
    <option value="mfwd">MFWD</option>
    <option value="4wd">4WD</option>

  </select>  </label>


</form>

<form method="post" action="A.php" id="form2">
</form>

<script>
  var drop=$('#drop');
  var form=$('#form2)

drop.change(function(){
  var a= drop.val();
  // here i show you how to add drop down value to form action
  form.attr('action',a);
})
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM