i want create little function blocks user selecting more 3 items multi select list. found useful things on web, allowed me launch alert when user selects more 3 items. however, can't find way remove last selected item.
so far, here's jquery:
$('.metiersmission').change(function(event) { if ($(this).val().length > 3) { alert('vous ne pouvez choisir que 3 métiers'); $(this).select2('val', ''); } }});
with code, if user selects more 3 items, code deletes everything user ever selected, not want. can't find anywhere, has idea?
thank in advance
you can listen change
event , count selected options. if selected options 3, disable other options:
$(document).on('change', 'select', function() { var = $(this); var val = that.val(); if (val.length > 2) { // find every unselected option , disable them that.find('option').not(':selected').attr('disabled','disabled'); } else { // enable options that.find('option').removeattr('disabled'); } });
select { width:250px; height:150px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <select multiple> <option value="volvo">volvo</option> <option value="saab">saab</option> <option value="opel">opel</option> <option value="audi">audi</option> <option value="citroen">citroen</option> <option value="peugeot">peugeot</option> </select>
Comments
Post a Comment