Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - How to hide dropdown on load jquery

Good Day,

I'm trying to understand how JQuery hide/ show and change elements work. In the example is 4 dropdowns with the change function on the first drop down 'Please Select'. Based on the option chosen the correct box appears. Looks good there. The only trouble I'm having is how to hide all but the parent box onload.

Fiddle: https://jsfiddle.net/wj_fiddle_playground/mt69f5w4/15/

Javascript Below:

$(document).ready(function() {

  $('#exampleFruit').change(function() {
    var val = $('#exampleFruit').val();
    $('.exampleSubselect').hide();
    if(val) {
        $('#example'+val).show();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jQuery methods works simple:

show() just adding css-property display: block

hide() just adding css-propery display: none

So you should just add style="display: none;" to your selects

Fiddle: https://jsfiddle.net/kodxg0y8/

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements</title>

</head>
<body>
<tr id="table1">
 <select id="exampleFruit">
    <option value="">-- Please Select --</option>
    <option>Apple</option>
    <option>Banana</option>
    <option>Orange</option>
</select>
</tr>

<tr id="table2">
<select id="exampleApple" class="exampleSubselect" style="display: none;">
    <option>Red Delicious</option>
    <option>Granny Smith</option>
    <option>Cox's Orange Pippin</option>
</select>
</tr>
<tr id="table3">
<select id="exampleBanana" class="exampleSubselect" style="display: none;">
    <option>Plantain</option>
    <option>Burro</option>
    <option>Cavendish</option>
</select>
</tr>
<tr id="table4">
<select id="exampleOrange" class="exampleSubselect" style="display: none;">
    <option>Blood</option>
    <option>Navel</option>
    <option>Valencia</option>
</select>
</tr>
</body>
</html>

$(document).ready(function() {
    $('#exampleFruit').change(function() {
        var val = $('#exampleFruit').val();
        $('.exampleSubselect').hide();
        if(val) {
            $('#example'+val).show();
        }
        
    });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...