Following the code
<select id="choice_option">
<option value="00">Hourly</option>
<option value="15">15 minutes</option>
<option value="30">30 minutes</option>
<option value="45">45 minutes</option>
</select>
<select id="from_time">
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
...
</select>
<select id="to_time">
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
...
</select>
<input type="checkbox" value="6:00" class="times" />6:00
<input type="checkbox" value="6:00" class="times" />6:15
<input type="checkbox" value="6:00" class="times" />6:30
<input type="checkbox" value="6:00" class="times" />6:45
<input type="checkbox" value="6:00" class="times" />7:00
<input type="checkbox" value="6:00" class="times" />7:15
<input type="checkbox" value="6:00" class="times" />7:30
<input type="checkbox" value="6:00" class="times" />7:45
...
var choice = document.getElementById('choice_option');
Event.add('choice_option', 'change', function() {
checkTimes(choice);
});
var checkTimes = function(elem) {
var from = document.getElementById('from_time'),
to = document.getElementById('to_time'),
times = document.querySelectorAll('.times'),
len = times.length,
i, t, startTime, stopTime;
for(i = 0; i < len; i++) {
t = times[i].value.slice(-2);
startTime = from.options[from.selectedIndex].value;
stopTime = to.options[to.selectedIndex].value;
if(times[i].checked) {
times[i].checked = false;
}
if(elem.options[elem.selectedIndex].value == t) {
times[i].checked = true;
}
}
}
What I want to achieve is set the check boxes
as checked between users's selected hours " from_time
" and " to_time
".
I made it till the point where check boxes get checked based on : 00, :15, :30
or 45 minutes. But I'm stuck when the checked boxes should be between selected times.
So if user chooses 15 minutes and from 06:00 to 14:00 I want every checkbox
which has the value 6:15, 7:15
, 8:15
... 14:15
to be checked.
I tried to compare the " from_time
" and " to_time
" but no luck so far.
Thank you
Why not take a simpler approach...
http://codepen.io/anon/pen/jEBQre
<select id="fromHour">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<select id="fromMinutes">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<span>to</span>
<select id="toHour">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<select id="toMinutes">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
I found it. What I should've done was this
instead of
if(elem.options[elem.selectedIndex].value == t) {
times[i].checked = true;
}
i changed to
if(startTime <= times[i].value && stopTime >= times[i].value
&& elem.options[elem.selectedIndex].value == t) {
times[i].checked = true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.