Reputation: 11
I'm trying to write a code that shows div boxes containing form elements based on a select box. I think the easiest way to explain this problem is to give an example.
http://jsfiddle.net/544rc/1/ If you look at this fiddle. Please try the following:
first select option 1, searchform 1 appears..which is right. select option 3, search form 1 and 2 appear, which is also right but now, when you select option 1 again. No form appears while it should show searchform 1.
I'm a rookie when it comes to jQuery so if my script isn't as pretty as it could be, you know the reason for that.
my jQuery:
$(document).ready(function() {
$("#select").change(function() {
var val = $(this).val();
if (val == "value1" || val == "value2") { //check if value 1 or 2 are selected
$("#search2").slideDown("fast"); //Slide Down Effect
$("#search1").slideUp("fast");
} else {
$("#search1").slideUp("fast"); //Slide Down Effect
$("#search1").slideUp("fast");
}
$("#select").change(function() {
var val = $(this).val();
if (val == "value3" || val == "value4" || val == "value5") { // check if value 3, 4 or 5 are selected
$("#search1").slideDown("fast"); //Slide Down Effect
$("#search2").slideDown("fast");
} else {
$("#search1").slideUp("fast"); //Slide Down Effect
$("#search2").slideUp("fast");
}
});
});
});
HTML:
<form id="indexsearch" method="post" action="#"> <!-- searchform -->
<div class="soortselect">
<label for="select">Ik zoek:</label>
<select name="select" id="select" > <!-- select box -->
<option value="">(choice)</option>
<option value="value1">option 1</option>
<option value="value2">option 2</option>
<option value="value3">option 3</option>
<option value="value4">option 4</option>
<option value="value5">option 5</option>
</select>
</div>
<div class="hide" id="search1"> <!--hidden search form 1 -->
<div class="soortselect">
<label for="edu">Opleiding</label>
<input name="edu" type="text" id="edu" ><br />
</div>
</div>
<div class="hide" id="search2"> <!-- hidden search form 2 -->
<div class="soortselect">
<label for="postal">Postcode</label>
<input name="postal" type="text" id="postal"><br />
<label for="name">Name</label>
<input name="name" type="text" id="name"><br />
<input name="vind2" type="submit" id="vind" value="Vind!">
</div>
</div>
</form>
Any ideas?
Upvotes: 1
Views: 772
Reputation: 997
Try this jsFiddle: http://jsfiddle.net/544rc/4/
Based on what I see, you just needed to add another else if statement (checking for value 3,4,5).
I added another else if to slide up when (choice)
is selected again.
Here's the updated Javascript:
$(document).ready(function() {
//On Select Change...
$("#select").change(function() {
//Get Current Value
var val = $(this).val();
//If the value is the 1st or 2nd dropdown...
if (val == "value1" || val == "value2") {
//Slide down the second search form and slide up the first
$("#search2").slideDown("fast"); //Slide Down Effect
$("#search1").slideUp("fast");
}
//If the value is the 3rd, 4th, or 5th dropdown...
else if(val == "value3" || val == "value4" || val == "value5") {
//Slide down both the forms
$("#search1").slideDown("fast"); //Slide Down Effect
$("#search2").slideDown("fast");
}
//If the value is nothing (choice is selected)...
else{
//Slide up both forms
$("#search1").slideUp("fast");
$("#search2").slideUp("fast");
}
});
});
I deleted your original else
statement because it wasn't needed.
Upvotes: 1