简体   繁体   中英

Show content of 2nd Drop down after 1st Drop down getting selected.

I've managed to get one drop-down list to populate the second drop-down list based on the selection.

HTML here:

    <select size="1" id="BodyPart" title="" name="BodyPart">
<option value="">-Select Body Part-</option>
<option value="chest">Chest</option>
<option value="biceps">Biceps</option>
</select>

<div class="container">

<div class="chest">
<select class="exercise_select">
<option value ="__select__">SELECT</option>
<option value ="chestpress">Chest Press</option>
<option value ="inclinechestpress">Incline Chest Press</option>
</select>


<div class="chest">
<div class="chestpress exercise_name">
CHEST PRESS
</div>
<div class="inclinechestpress exercise_name">
INCLINE CHEST PRESS    
</div>
</div>
</div>

<div class="biceps">
<select class="exercise_select">
<option value ="__select__">SELECT</option>
<option value="bicepcurls">Bicep Curls</option>
<option value="hammercurls">Hammer Curls</option>
</select>

<div class="biceps">
<div class="bicepcurls exercise_name">
BICEP CURLS
</div>
<div class="hammercurls exercise_name">
HAMMER CURLS   
</div>
</div>
</div>

JavaScript here:

<Script language='JavaScript'>
$(document).ready(function() {
    var elements = $('div.container').children().hide();
    $('#BodyPart').change(function() {
        elements.hide();
        var value = $(this).val();
        if (value.length) { // if somethings' selected
            elements.filter('.' + value).show(); // show the ones we want
    }
});

    $('.exercise_select').change(function(){
        console.log(123);
        $('.exercise_name').hide();
        var subele=$('.exercise_name');
        subele.filter('.'+$(this).val()).show();
    });
});
</script>

As you can see, when 'Chest' or 'Biceps' is selected, relative exercises to that body part becomes available to choose in the second drop-down list. I want to display information specific to that second selection (as you can see I've used the exercise's title in capitals for dummy text), but for some reason everything displays UNTIL a second option is selected.

So for example, I'd choose 'Chest', then before I select any of the two exercises in the second drop-down list, both dummy texts appear. They only single out once I choose the exercise. Is there a way to have those dummy texts remain invisible until one of the options is selected?

Many thanks for your time.

Include $('.exercise_name').hide(); in first dropdown change event

$('#BodyPart').change(function() {
    elements.hide();
    var value = $(this).val();
    if (value.length) { // if somethings' selected
        elements.filter('.' + value).show(); // show the ones we want
}
    $('.exercise_name').hide();
});

Demo

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