Я хотел бы создать функцию, в которой пользователь не должен иметь возможность выбирать значение, превышающее «n», из нескольких вариантов выбора. В моем случае я хочу ограничить пользователя выбором значения не более 4.
HTML:
<ul class="numberOfElementsList">
<li>0</li>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ul>
<select>
<ul class="numberOfElementsList">
<li>0</li>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ul>
<ul class="numberOfElementsList">
<li>0</li>
<li class="active">1</li>
<li>2</li>
<li>3</li>
</ul>
jQuery:
if(numBer > 4) {
alert('You can not select more then 4.');
} else if(numBer == 4) {
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li.active').nextAll('li').hide();
$(this).nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li.active').nextAll('li').hide();
} else if(numBer == 3) {
if (indexNum == 3){
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(2)').nextAll('li').hide();
$(this).nextAll('li.active').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li::nth-child(2)').nextAll('li').hide();
} else {
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li.active, li:last-child').nextAll('li').hide();
$(this).nextAll('li.active').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li.active, li:last-child').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(3)').prevAll('li').show();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li:nth-child(3)').prevAll('li').show();
}
} else if(numBer == 2) {
if (indexNum == 2){
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(3)').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').find('li:last-child').hide();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li:nth-child(3)').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(4)').prevAll('li').show();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li:nth-child(4)').prevAll('li').show();
} else {
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(3)').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').find('li:last-child').hide();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li:nth-child(3)').nextAll('li').hide();
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li:nth-child(4)').prevAll('li').show();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li:nth-child(4)').prevAll('li').show();
}
} else {
$(this).closest('.breedOptionsWrapper').nextAll('.breedOptionsWrapper').find('li').nextAll('li').show();
$(this).prevAll('li').show();
$(this).nextAll('li').show();
$(this).closest('.breedOptionsWrapper').prevAll('.breedOptionsWrapper').find('li').nextAll('li').show();
}
Здесь я хочу, чтобы пользователь ограничил максимум 4. Например, если пользователь выбирает комбинацию 1-2-1, 1-1-2, 3-1-0 или любую другую комбинацию, тогда другие значения параметров должны быть отключены. Я много чего пробовал, но не работал.
Спасибо