3
How do I make to when I select the Leader option, the javascript do not allow another function to be selected (multiselect)?
The scenario is as follows: Each select represents the functions of each user.
The user who is selected as Leader, cannot have any other function; only that of Leader.
If the option is different from Leader, then javascript allows selecting more than one function.
Here is my attempt:
$("select").on("change", function() {
var self = $(this);
var values = self.val();
$("select").not(self).each(function() {
var _values = $(this).val();
if (_values == "1") {
$(this).selectpicker('deselectAll')
for (var v = _values.length; v--;) {
if (values.indexOf(_values[v]) >= 0) {
_values.splice(v, 1);
}
}
$(this).val(_values);
}
});
$(".selectpicker").selectpicker("refresh");
});
<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>
Thank you so much for your help. But would it be like when a select(father) is selected as "Leader" and another select(daughter) when I click on "Leader" as well, the javascript 'deselect' the Leader of select(father)? You could help me?
– André Albson
In case there would be at most only one selected leader in all selects?
– JuniorNunes
Exactly that
– André Albson
I think this would fit better in a new question, since the initial question does not detail this very well, edit the initial question or create a new one for me to help you.
– JuniorNunes
All right, I’ll edit the question
– André Albson
Edited question
– André Albson
I edited the answer, see if it helps you!
– JuniorNunes
Thanks @Juniornunes. The result was just that! Thanks!
– André Albson