How do I not select select an option already selected in another select?

3

How to make select not allow that the option "Lider" is not duplicate , because in my business rule only one user can be a leader .

I thought of the following:

Assuming I have 3 selects. If I select one of them as LEADER , and select another select also as LEADER , javascript deselected the leader option of the first select and leave selected only the second select Leader option.

Here is a simulation of the problem:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script><linkhref="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet" />

<select class="selectpicker" multiple>
      <option value="1">Líder</option>
      <option value="10">Para Conhecimentor</option>
      <option value="11">Participante</option>
    </select>
    <br><br>
    <select class="selectpicker" multiple>
      <option value="1">Líder</option>
      <option value="10">Para Conhecimentor</option>
      <option value="11">Participante</option>
    </select>
    <br><br>
    <select class="selectpicker" multiple>
      <option value="1">Líder</option>
      <option value="10">Para Conhecimentor</option>
      <option value="11">Participante</option>
    </select>
    <br><br>
    <select class="selectpicker" multiple>
      <option value="1">Líder</option>
      <option value="10">Para Conhecimentor</option>
      <option value="11">Participante</option>
    </select>
    
asked by anonymous 29.11.2016 / 18:12

1 answer

3

See if this solves your problem:

$(".selectpicker").on("change", function() {
    var self = $(this);
    var values = self.val();

    $(".selectpicker").not(self).each(function() {
        var _values = $(this).val();
        for (var v = _values.length; v--;) {
            if (values.indexOf(_values[v]) >= 0) {
                _values.splice(v, 1);
            }
        }

        $(this).val(_values);
    });
});
    
29.11.2016 / 18:48