Выбрать какие чекбоксы неактивны
Подскажите, как сделать проверку. Если у меня выбраны один или несколько чекбоксов filter=one, а все остальные, ни один из них, не выбраны, то показать сообщение.
$('button').on('click', function() {
if ( $('input[filter=one]:checked') && $('input:not([filter=one]):not(:checked)')) {
alert(1);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<br/>
<input filter="two" type="checkbox">
<input filter="two" type="checkbox" checked>
<input filter="two" type="checkbox">
<input filter="two" type="checkbox">
<br/>
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<button>Проверить</button>
Ответы (1 шт):
Автор решения: Laukhin Andrey
→ Ссылка
Истина должна быть в том случае, если есть хотя бы один выбранный чекбокс в первой группе И ноль выбранных в остальных группах.
$('button').on('click', function() {
let check1 = $('input[filter=one]:checked');
let check2 = $('input:not([filter=one]):checked');
if (check1.length > 0 && check2.length == 0) alert(1);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<input filter="one" type="checkbox" checked>
<br/>
<input filter="two" type="checkbox">
<input filter="two" type="checkbox" checked>
<input filter="two" type="checkbox">
<input filter="two" type="checkbox">
<br/>
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<input filter="three" type="checkbox">
<button>Проверить</button>