I am working on a search form that can filter category, sub category. This is my html code:
<select id="category" name="category">
<option value="0">-- All Categories --</option>
<option value="one">category one</option>
<option value="two">category two</option>
</select>
<select id="subcategory" name="subcategory">
<option value="0">-- All Sub Categories --</option>
</select>
I used JQuery to filter sub category
$('#category').on("change",function(){
var cat = $(this).find("option:selected").text();
var subCat = "";
switch (cat) {
case "category one":
subCat = '<option value="sub1">Sub Category One</option>';
break;
case "category two":
subCat = '<option value="sub2">Sub Category Two</option>';
break;
}
$("#subcategory").html(subCat);
})
It works fine with filtering. However, I submitted the search form and went to a result page, then when I clicked browser "go back" button. Sub category became "-- All Sub Categories --" again, no data was saved. Not until I made another selection to trigger on change event, nothing was displayed. How do I improve my answer?