简体   繁体   中英

How to Dynamically Update the Values of a 'select-chosen' class Drop down on the Base of Other 'select-chosen' Drop down?

I have two drop downs. If I don't use the select-chosen class and use them simply as form-control class, their HTML is simple and I can change the values of the 2nd drop down by sending the AJAX request based on the value of 1st drop down as given in below code snippet:

<select id="firsttDropDown" class='form-control'>
    <option value='1'>Value 1</option>
</select>

<select id="secondDropDown" class='form-control'>
    <option value='2'>Value 2</option>
</select>
$("#firstDropDown").on('change', function(e) {
    var loc_id = e.target.value;
    $.get('my/url/', function(data) {
        // success data
        $('#secondtDropDown').empty();
        $.each(data.list, function(index, eObj){
            $('#secondtDropDown').append('<option value=' + eObj.id + '>' + eObj.first_name + ' ' + eObj.last_name + '</option>');
        });
    });
});

However, when I want to make these drop downs look nicer using select-chosen class, everything goes wrong. This class totally changes the HTML of my drop downs like this.

<select id="firstDropDown" class="select-chosen" name="firstDropDown" data-placeholder="Choose a ..." style="display: none;">
    <option value="1">Value 1</option>
</select>
<div class="chosen-container chosen-container-single" style="width: 100%;" title="" id="firstDropDown_chosen">
    <a class="chosen-single" tabindex="-1">
        <span>Vlaue 1</span>
        <div><b></b></div>
    </a>
    <div class="chosen-drop">
       <div class="chosen-search">
          <input type="text" autocomplete="off">
       </div>
       <ul  class="chosen-results">
           <li class="active-result result-selected" data-option-array-index="0">
               Value 1
           </li>
       </ul>
    </div>
</div>

<select id="secondDropDown" class="select-chosen" name="Visitor_user_id" style="display: none;">
    <option value="71">Andrew Simond</option>
</select>
<div class="chosen-container chosen-container-single" style="width: 100%;" title="" id="secondDropDown_chosen">
    <a class="chosen-single" tabindex="-1">
        <span>Value 1</span>
        <div><b></b></div>
    </a>
    <div class="chosen-drop">
        <div class="chosen-search">
            <input type="text" autocomplete="off">
        </div>
        <ul class="chosen-results">
            <li class="active-result result-selected" data-option-array-index="0">Andrew Simond</li>
            <li class="active-result" data-option-array-index="1">Value 1</li>
            <li class="active-result" data-option-array-index="2">Value 2</li>
            <li class="active-result" data-option-array-index="3">Value 3</li>
            <li class="active-result" data-option-array-index="4">Value 4</li>
        </ul>
    </div>

Now my question is that how can I handle all this odd (for me, not for all of you) looking HTML to update the dropdown from an ajax request as I did in above code snippet?

Once we set the value. we have to refresh the select box.

$('select').val(123);
$('select').trigger("liszt:updated");

I hope this will work.

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