Answer the question
In order to leave comments, you need to log in
When choosing a certain option, put down the disabled property for the rest?
<select id="status" class="form-control select2" name="status" multiple onchange='myStatus()'>
<option value='-222'>Просрочено</option>
<option value='1'>В работе</option>
<option value='2'>Выполнено</option>
</select>
function myStatus() {
var op = document.getElementById("status");
for (var i = 0; i < op.length; i++) {
(op[i].value.toLowerCase() != "-222") ? op[i].disabled = true : op[i].disabled = false;
}
}
Answer the question
In order to leave comments, you need to log in
const select = document.querySelector('#status');
select.addEventListener('change', function() {
const val = this.value;
const expiredVal = '-222';
const isExpired = val === expiredVal;
[...this.options].forEach(n => n.disabled = val && (
(n.value === expiredVal && !isExpired) ||
(n.value !== expiredVal && isExpired)
));
});
// это для отмены - сделайте кнопку <button id="reset">сбросить</button>
document.querySelector('#reset').addEventListener('click', function() {
select.value = null;
select.dispatchEvent(new Event('change'));
});
Why do you need disabled, you don't need to do that, just deselect the type
document.querySelector("#status").addEventListener("change",function (evt) {
Array.from(evt.target.children).forEach((e)=>{
if(evt.srcElement.value == "-222" && e.value != "-222" || evt.srcElement.value != "-222" && e.value == "-222")
e.selected = false;
});
});
document.querySelector("#status").addEventListener("change", evt=> {
[...evt.target.children].forEach(e=> e.selected =e.selected && !(evt.srcElement.value == "-222" && e.value != "-222" || evt.srcElement.value != "-222" && e.value == "-222") );
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question