Reputation: 145
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.
if rear Tire and (2WD or 4wd) Selected i want to change action
value to 'A.php`
if Rear Tire and (MFWD) Selected i want to change action
value to 'B.php`
if Front Tire and (2WD or $wd) Selected i want to change action
value to 'C.php`
if front Tire and (MFWD) Selected i want to change action
value to 'D.php`
Upvotes: 0
Views: 1815
Reputation: 1758
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.
Upvotes: 1
Reputation: 131
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>
Upvotes: 1