Если вы хотите выполнить свои условия, где:
- Элемент
#static
имеет значение '10'
и - Первые три элемента
.sample
имеют всезначение '1'
,
... вы можете использовать следующее условие:
// Get first 3 `.sample` elements, and see if their values are '1'
const $matchingSamples = $('.sample').filter(function(i) {
return i <= 2 && this.value === '1';
});
// Combine all your conditions
const condition = $('#static').val() === '10' && $matchingSamples.length === 3;
Переменная condition
будет эквивалентна:
$('#static').val() === '10' &&
$('.sample:eq(0)').val() === '1') &&
$('.sample:eq(1)').val() === '1') &&
$('.sample:eq(2)').val() === '1')
Конечно, вы можете переписать приведенную выше логику, чтобы вообще не использовать jQuery, в ES6:
const samples = document.querySelectorAll('.sample');
const matchingSamples = Array.from(samples).filter((sample, i) => {
return i <= 2 && sample.value === '1';
})
const condition = document.getElementById('static').value === '10' && matchingSamples.length === 3;
См. Подтверждение концепции ниже:
const numberOfSamplesToCheck = 3;
function checkValues() {
const $matchingSamples = $('.sample').filter(function(i) {
return i <= numberOfSamplesToCheck - 1 && this.value === '1';
});
const condition = $('#static').val() === '10' && $matchingSamples.length === numberOfSamplesToCheck;
console.log(condition);
}
checkValues();
$(':input').on('change', checkValues);
input {
border: 1px solid #000;
width: 75px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="static" value="10" type="number" />
<input class="sample" value="1" type="number" />
<input class="sample" value="1" type="number" />
<input class="sample" value="1" type="number" />