Я хочу установить флажок на основе даты начала (дня) и даты окончания (дня) , и я хочу снять флажок, если не соответствует дате начала (дня) и дата окончания (день) . но он только проверяет один из них.
$(function() {
$('#startDate, #endDate').on('change', function() {
let startDate = moment($('#startDate').val(), 'mm-dd-yyyy').format('YYYY-MM-DD')
let endDate = moment($('#endDate').val(), 'mm-dd-yyyy').format('YYYY-MM-DD')
let from = new Date(startDate)
let to = new Date(endDate)
let days = [
'minggu', 'senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu'
];
let d = from;
while (d <= to) {
let daysToLowerCase = days[d.getDay()]
$('.days').each(function(key, value) {
if (this.id == daysToLowerCase) {
$(`#${this.id}`).prop('checked', true)
}
if (this.id != daysToLowerCase) {
$(`#${this.id}`).prop('checked', false)
}
})
d = new Date(d.getTime() + (24 * 60 * 60 * 1000));
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6">
<div class="form-group">
StartDate
<input type="date" id="startDate">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
EndDate
<input type="date" id="endDate">
</div>
</div>
</div>
<table class="table">
<tr>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="senin" value="SENIN">
<label for="senin">
Senin
</label>
</div>
</th>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="selasa" value="SELASA">
<label for="selasa">
Selasa
</label>
</div>
</th>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="rabu" value="RABU">
<label for="rabu">
Rabu
</label>
</div>
</th>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="kamis" value="KAMIS">
<label for="kamis">
Kamis
</label>
</div>
</th>
</tr>
<tr>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="jumat" value="JUMAT">
<label for="jumat">
Jumat
</label>
</div>
</th>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="sabtu" value="SABTU">
<label for="sabtu">
Sabtu
</label>
</div>
</th>
<th>
<div class="icheck-success">
<input type="checkbox" class="days" name="days[]" id="minggu" value="MINGGU">
<label for="minggu">
Minggu
</label>
</div>
</th>
</tr>