简体   繁体   中英

adding search option to the select list

I have two multiselect lists that can interchange the list values with each other by clicking them. I tried to add search functionality to the filled list (select-list) which works fine until the move I click a list element. even though the value is removed from the list still the search option adds that moved element in it. Check the demo( http://jsfiddle.net/cs6Xb/99/ ).

//code is:

//html-
<input id="someinput"><br/>
    <select class="select-cities" name="city" id="optlist" multiple="multiple">
        <option>Frederiksberg</option>
        <option>Vanløse</option>
        <option>Glostrup</option>
        <option>Brøndby</option>
        <option>Roskilde</option>
        <option>Køge</option>
        <option>Gentofte</option>
        <option>Hillerød</option>
        <option>Tårnby</option>
        <option>Vallensbæk</option>
    </select>
</input>
<br/>
<select class="chosen-cities" name="chosen-cities-name" multiple="multiple">
</select>

//jquery:

$(function() { var opts = $('#optlist option').map(function(){
    return [[this.value, $(this).text()]];
  });


   $('#someinput').keyup(function(){
     var rxp = new RegExp($('#someinput').val(), 'i');
     var optlist = $('#optlist').empty();
     opts.each(function(){
         if (rxp.test(this[1])) {
             optlist.append($('<option/>').attr('value', this[0]).text(this[1]));
      }
  });

});



    $('.select-cities').click(function () { $('.select-cities option:selected').remove().appendTo('.chosen-cities');});

    $('.chosen-cities').click(function () {
    $('.chosen-cities option:selected').remove().appendTo('.select-cities');
    });

});
  I 

This is because you've cached the values in opts once document is loaded and is not updating it when the select gets updated.

You have to re-evaluate opts to get the new set of values once an item is moved from the list..

$('.select-cities').click(function () {
    $('.select-cities option:selected').remove().appendTo('.chosen-cities');
    opts = $('#optlist option').map(function () {
        return [[this.value, $(this).text()]];
    });
});

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