I did something similar, but with radio buttons. Hope it helps:
function reset_last_checkboxes() {
$("#export_last_month").attr("checked", false);
$("#export_3_last_months").attr("checked", false);
$("#export_last_year").attr("checked", false);
}
var date_format = "DD, d MM, yy";
$("#id_from_date").attr("readonly", "readonly");
$("#id_till_date").attr("readonly", "readonly");
$("#id_from_date").datepicker();
$("#id_till_date").datepicker();
$( "#id_from_date" ).change(function() {
$( "#id_from_date" ).datepicker( "option", "dateFormat", date_format );
});
$( "#id_till_date" ).change(function() {
$( "#id_till_date" ).datepicker( "option", "dateFormat", date_format );
});
$( "#id_from_date" ).click(function() {
reset_last_checkboxes();
});
$( "#id_till_date" ).click(function() {
reset_last_checkboxes();
});
$("#id_till_date").val(format_date(new Date(now)));
$("#export_last_month").click(function(){
var dt = new Date(now);
$("#id_till_date").val(format_date(dt));
$("#id_from_date").val(format_date(prevMonth(dt)));
});
$("#export_3_last_months").click(function(){
var dt = new Date(now);
$("#id_till_date").val(format_date(dt));
$("#id_from_date").val(format_date(prevMonth(prevMonth(prevMonth(dt)))));
});
$("#export_last_year").click(function(){
var dt = new Date(now);
$("#id_till_date").val(format_date(dt));
for (var i=0;i<12;i++) {
dt = prevMonth(dt);
}
$("#id_from_date").val(format_date(dt));
});
You can always replace my radio buttons with drop down menu and write this code better than me :)
By the way, prevMonth I calculate this way:
function prevMonth(dt){
var thisMonth = dt.getMonth();
dt.setMonth(thisMonth-1);
if(dt.getMonth() != thisMonth-1 && (dt.getMonth() != 11 || (thisMonth == 11 && dt.getDate() == 1)))
dt.setDate(0);
return dt;
}