How do SELECT not allow selecting an option already selected in another select?

Asked

Viewed 861 times

3

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

I thought the following:

Assuming I have 3 selects. If I select one of them as LEADER, and select another select as well LEADER, the javascript unselected the leading option of the first select and would leave selected only the Leader option of the second select.

Here is a simulation of the problem:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="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>
<link href="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>

  • It’s not an answer to the question, but wouldn’t it be simpler to have a radio button to define the leader? Or three columns with names, being the first leader, select simple, and the other two select multiple for knowledge and participant.

  • It would be interesting also @Bacco, but in the documentation is not allowed radiobutton... I need to actually do with selects...

  • Is that so? http://answall.com/a/168517/32827

  • Very close to this @Lucascosta, but would only disable the value option "1". But it has helped a lot. Obigado.

1 answer

3


See if that 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);
    });
});
  • Thank you so much for your help! You gave me a great head start. But some selects are still allowed to have two selects... check out https://jsfiddle.net/andrealbson/5t8noxne/

  • this happens because it is necessary to execute the refresh in the plugin after the changes, follow correction: https://jsfiddle.net/5t8noxne/1/

  • True, @Andrenato. I had put the refresh in the wrong corner.

  • Now a final call for help if you can: how to prevent the select that is selected as a "Leader" from having more than one function? Only that of leader?

  • What do you mean? The guy who’s the leader, he can’t have another job?

  • that’s right, whoever is a leader cannot have another function

  • ok! at a glance, https://jsfiddle.net/5t8noxne/4/

  • Thank you very much! The expected result was just that!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.