简体   繁体   中英

How to disable <option> according to <option selected>?

I have a list of users and each has a role.

The user who is selected as "Líder" can not have more than one role, only "Líder".

If the user selects another option (daughters) the option "Líder" should be disabled. Users who are not "Líder" can have more than one role.

Here is a simulation of the problem: jsfiddle

HTML:

<select class="selectpicker" id="funcao" multiple data-max-options="1">
  <option value="lider">Líder</option>
  <option value="conhecimento">Para Conhecimentor</option>
  <option value="participante">Participante</option>
</select>
<br><br>
<select class="selectpicker" id="funcao" multiple data-max-options="1">
  <option value="lider">Líder</option>
  <option value="conhecimento">Para Conhecimentor</option>
  <option value="participante">Participante</option>
</select>
<br><br>
<select class="selectpicker" id="funcao" multiple data-max-options="1">
  <option value="lider">Líder</option>
  <option value="conhecimento">Para Conhecimentor</option>
  <option value="participante">Participante</option>
</select>
<br><br>
<select class="selectpicker" id="funcao" multiple data-max-options="1">
  <option value="lider">Líder</option>
  <option value="conhecimento">Para Conhecimentor</option>
  <option value="participante">Participante</option>
</select>

JS:

$('select').change(function(){
    var sel = $(this); 
    var data = sel.data('prev'); 
    var val = sel.val();
    var prev; 
    if(data){ prev = data.val; }
    sel.data('prev', {val: val}); 
    sel.nextAll().each(function(){
        if(prev){
            $(this).find("[value='" + prev+ "']").prop("disabled",false);
            $('.selectpicker').selectpicker('refresh');
        }
        $(this).find("[value='" + val + "']").prop("disabled",true);
        $('.selectpicker').selectpicker('refresh');
    });
    $('.selectpicker').selectpicker('refresh');
});

You can check the effect obtained with the code below - jsfiddle

$('select').change(function() {
    var sel = $(this);
    disableThis(sel);
    $('.selectpicker').selectpicker('refresh');
});

function disableThis(sel) {
    var temSelecionado = false;

    $("option[value='1']").each(function() {
        if (this.selected) {
            temSelecionado = true;

            $(this).parent().each(function() {
                $(this.options).each(function() {
                    if ($(this).val() != "1") {
                        $(this).prop("disabled", true);
                    }
                })
            });
        }
        else {
                $(this).parent().each(function() {
                $(this.options).each(function() {
                    if ($(this).val() != "1") {
                        $(this).prop("disabled", false);
                    }
                })
            });
        }
    }); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM