jquery - Change the form action attribute taken from values in two selects -
i've found similar question here: javascript - change form action based on selection?
but problem different. have 2 selects:
<form id="carsearch" action="" method="post"> <select name="style" id="style"> <option value="" selected>any style</option> <option value="style/sports-1">sports</option> <option value="style/wagon-3">wagon</option> <option value="style/suv-6">suv</option> </select> <select name="make" id="make"> <option value="" selected>any make</option> <option value="make/porsche-3">porsche</option> <option value="make/ford-6">ford</option> <option value="make/volvo-7">volvo</option> </select> <input type="submit" value="submit"/> </form>
i want build form action attribute based on values in both selects. here situations:
- if both style , make set "any" , hence have no values; form action should blank.
- if style specified, make isn't; form action should just value style e.g.
action="style/sports-1"
- if make specified, style isn't; form action should just value make e.g.
action="make/volvo-7"
- if both style , make specified; form action should concatenation of values style , make e.g.
action="/style/sports-1/make/volvo-7"
the defaults both selects "any". try create 2 variables in jquery each select , set values whenever either select changes. concatenation part that's confusing me.
i have created jsfiddle here me started http://jsfiddle.net/l91rb9f9/3/
i'm ending 2 slashes when style isn't specified.
try :
$("#carsearch").submit(function () { var style = $("#style").val(); var make = $("#make").val(); if (style == "" && make == ""){ $('form').get(0).setattribute('action', ''); } else if (style != "" && make != "") { $('form').get(0).setattribute('action', style + "/" + make); } else { $('form').get(0).setattribute('action', style + make); } });
Comments
Post a Comment