我正在使用以下内容创建日历,我可以为其选择多个工作日。它使用一个对象(根据选择的日期设置为1/0 true / false)来存储打开哪些日期。
然后我有一个函数,可以在每天打开的菜单上添加一个“选定的”类。当我手动单击日期时,它可以正常工作,但是我创建了用于选择特定日期的按钮(或者,如果可以使用,则可以选择日期范围),但是出于某种原因,突出显示日期的代码仅在直接单击时才有效使用按钮。
真正的雀斑是怎么回事?
<div id="days_cal_area">
<div id="days_cal"></div>
<div id="last_day" >Last</div>
</div>
<script>
// Maintain array of dates
var days = {};
$('#days_cal').datepicker({
inline: true,firstDay: 0,showOtherMonths: false,selectOtherMonths: false,dateFormat: 'yy-mm-dd',changeYear: false,defaultDate: new Date(1995,1),onSelect: function (dateText,inst) {
day = parseInt(dateText.split('-')[2])
days[day] = !days[day];
daysStr();
},beforeShowDay: function (date) {
var day = date.getDate();
if (days[day])
return [true," date_selected day_"+day];
return [true,"day_"+day];
}
});
</script>
<div class="button_area row_items days_buttons">
<div class="sm_button" select="1" type="day">The 1st</div>
<div class="sm_button" select="15" type="day">The 15th</div>
<div class="sm_button" id='last_button'>Last day</div>
</div>
<div class="hidden_field">
<?php $task_form->write_fields(['days','weekdays','weeks','months']); ?>
</div>
<script>
function daysStr(){
console.log(days);
// Clear selected days
$('#days_cal .date_selected').removeclass('date_selected');
// Build a string for the form
temp = [];
$.each(days,function(day,addIt){
if (addIt) {
// Add it to the string for form submission
temp.push(day);
// Also mark it on the calendar
$('[data-day='+day+']').addClass('date_selected');
}
})
// Now add it to the form
$('#days_area [name=days]').val(temp.join(','));
}
$(document).ready(function(){
// prep the calendar (ONE TIMEM THINGS)
$('#days_area a').each(function(){
theTD = $(this).closest('td');
day = $(this).text();
$(theTD).data('day',day);
})
$('#days_area [type=day]').click(function(){
tempDays = $(this).attr('select').split(','); // Comma list of days
$(tempDays).each(function(index,day){
days[day] = true;
})
daysStr();
})
$('#last_button').click(function(){
$(this).toggleclass('on');
if ($(this).hasClass('on')){
$('#last_day').show();
days['last'] = true;
}
else {
$('#last_day').hide();
days['last'] = false;
}
daysStr();
})
})
</script>