B
B
BuBux2020-04-13 16:36:00
JavaScript
BuBux, 2020-04-13 16:36:00

How, when choosing one of the option select multiple, put disabled on the others?

There is a select multiple, and when choosing an option with a value of "null", it is necessary that all the others become disabled.

<div class="form-group col-md-6">
  <label for="field-1" class="control-label">Клиент</label>
        <?php if (is_array($clients)): ?>
        	<select id="client" class="form-control select2" name="client[]" style="width: 100%;" multiple onchange="ChangeFilters()">
                	<option value="null" <?= isset($saved_client) && in_array('null', $saved_client) ? 'selected':'' ?> <?= (!isset($saved_client)) && in_array('null', $s_cl) ? 'selected':'' ?>>Без клиента</option>
                        <?php foreach ($clients as $client): ?>
                                <option value="<?=$client['ID']?>" <?= isset($saved_client) && in_array($client['ID'], $saved_client) ? 'selected':'' ?> <?= (!isset($saved_client)) && in_array($client['ID'], $s_cl) ? 'selected':'' ?>>
                                        <?php echo $client['NAME']; ?>
        </option>
      <?php endforeach; ?>
        	</select>
  <?php endif; ?>
</div>

js file:
function ChangeFilters() {
  $('select#client').each(function () {
    var val = $(this).val();
    
    if (val != 'null') {
      $('select#client option[value="' + val + '"]').attr("disabled", true);
    }
  });
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question