简体   繁体   中英

Dynamically select Drop Down in Jquery

I have 4 Drop Downs.

图片

Each drop by default has a --select-- option. Each box has a unique id. As you can see, the second drop down is disabled if the above drop down value is --select--. It will only enable when the value is anything but --select--

Here's my code:

$(document).ready(function() {

$('#idOfTheFirstDropDown').bind('change', function() {
    var options = $(this).children('option');
    var item = $('div.c-select').children().hide(); // hide all the elements
    var value = Math.floor(Math.random() * options.length );
    //console.log(value);
    if (value.length) { // if selected 
        item.show(); // show the next dropdown
    }
}).trigger('change');
});

I want it to show the next dropdown only when my previous drop down value is not --select--. My code is taking in the first drop down but not changing the value. What am I doing wrong? Thanks.

My HTML for one drop down box. Only the ID's change for the remaining 3. Rest of the HTML remains the same.

<div class="c-select">
<select name="list1" onchange="javascript:setTimeout('__doPostBack(\'list1\',\'\')', 0)" id="idOfTheFirstDropDown" class="GroupDD dropDown ">
<option selected="selected" value="">-- Select --</option>
<option value="1">Group1</option>
</select>
</div>

Rather than hiding the options, I would use the disable attribute (comments in code):

 var selects = $('.drop-down'); selects.not(':eq(0)').prop('disabled', true); // disable all but first drop down selects.on('change', function() { var select = $(this), currentIndex = selects.index(select), nextIndex = currentIndex + 1; // only do this if it is not last select if (currentIndex != selects.length - 1) { selects.slice(nextIndex) // get all selects after current one .val('') // reset value .prop('disabled', true); // disable selects.eq(nextIndex).prop('disabled', select.val() === ''); // disable / enable next select based on val } })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="drop-down"> <option value="">--Select--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select><br> <select class="drop-down"> <option value="">--Select--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select><br> <select class="drop-down"> <option value="">--Select--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select><br> <select class="drop-down"> <option value="">--Select--</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select>

Check this one;

HTML code:

<select id="sel-1"></select>
<select id="sel-2"></select>
<select id="sel-3"></select>
<select id="sel-4"></select>

JS Code:

$(document).ready(function(){

     $("[id^=sel]").change(function(){

           /*
           *  find out the current box id
           */

           var id=$(this).attr("id");
           /*
           *  find out the current box id order number
           */

           var ordernumber=id.split("-")[1];

           if($(this).val()!="select")
           {
                 //enable next one
                 $("#sel-"+(ordernumber+1)).show();
           }
     })
})

Assuming the option that has '--select--' as text has no value, just use val on the handler to check if the selected option is other than the empty

if(this.val() !== '') {
// enable following select
}

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <style> select { margin: 5px 0; display: block; } </style> <script> $(function() { $('.cls-select').change(function() { if ($(this).val() == '--select--') { // reset and disable the next drop downs $('.cls-select:gt(' + $('.cls-select').index($(this)) + ')').val('--select--').prop('disabled', true) } else { // current drop down has value so enable the next drop down $(this).next().prop('disabled', false); } }); }) </script> <select class="cls-select"> <option>--select--</option> <option>one</option> <option>two</option> </select> <select class="cls-select" disabled> <option>--select--</option> <option>one</option> <option>two</option> </select> <select class="cls-select" disabled> <option>--select--</option> <option>one</option> <option>two</option> </select> <select class="cls-select" disabled> <option>--select--</option> <option>one</option> <option>two</option> </select>

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