Bootstrap Multiple Issue

I created a popup dropdown using Bootstrap Multiselect . I need to set a restriction in the choice of options (here I set it to 5), and if the limit is reached, I just turned off the other selection functions, and it works fine. But the problem is that I tried to select more than 5 using the SHIFT key. This does not work (means that my jQuery forbids the choice not to work) and I can choose more than 5. Please check the snippet and give me a solution.

JSFIDDLE

 jQuery('#soft_skill_list').multiselect({
        enableFiltering: true,
        maxHeight:400,
        enableCaseInsensitiveFiltering:true, 
        nonSelectedText: 'Soft Skills *', 
        numberDisplayed: 2, 
        selectAll: false, 
        onChange: function(option, checked) {
                // Get selected options.
                var selectedOptions = jQuery('#soft_skill_list option:selected');
 
                if (selectedOptions.length >= 5) {
                    // Disable all other checkboxes.
                    var nonSelectedOptions = jQuery('#soft_skill_list option').filter(function() {
                        return !jQuery(this).is(':selected');
                    });
 
                    nonSelectedOptions.each(function() {
                        var input = jQuery('input[value="' + jQuery(this).val() + '"]');
                        input.prop('disabled', true);
                        input.parent('li').addClass('disabled');
                    });
                }
                else {
                    // Enable all checkboxes.
                    jQuery('#soft_skill_list option').each(function() {
                        var input = jQuery('input[value="' + jQuery(this).val() + '"]');
                        input.prop('disabled', false);
                        input.parent('li').addClass('disabled');
                    });
                }
            }});
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" />

<select name="soft_skill_list[]" class="soft_skill_list"  id="soft_skill_list" multiple="multiple">
  <option>Analysing data</option>
  <option>Banquets Operations</option>
  <option>Concierge Operations</option>
  <option>Customer service experience</option>
  <option>Measuring and calculating</option>
  <option>Micros</option>
  <option>Numeracy Skills </option>
  <option>Opening Hotels</option>
  <option>Opera</option>
  <option>Outside catering</option>
  <option>Pre-opening</option>
  <option>Procedures </option>
  <option>Proficiency in computer programming</option>
  <option>Public speaking experience </option>
  <option>Reservation</option>
  <option>Restaurants operations</option>
  <option>Revenue Analysis</option>
  <option>Rooms Division</option>
  <option>Safety and Security</option>
  <option>Sales administration</option>
  <option>Sales Operations</option>
  <option>Social Media</option>
</select>
Run codeHide result

Please check the image. Attached Image

+4
source share
2 answers

Shift . jQuery.

, β†’ JSFIDDLE

var shiftClick = jQuery.Event("click");
shiftClick.shiftKey = true;

    $(".multiselect-container li *").click(function(event) {
        if (event.shiftKey) {
           //alert("Shift key is pressed");
            event.preventDefault();
            return false;
        }
        else {
            //alert('No shift hey');
        }
    });
+3

, 5 .

if (selectedOptions.length >= 5) {
  if (selectedOptions.length > 5) {
    // Show error
  } else {
    // What you do already
  }

, 5.

0

Source: https://habr.com/ru/post/1678112/


All Articles