admin管理员组文章数量:1310059
I've created a date_select in Rails (which is 3 select boxes: one for year, one for month, and one for day).
To have 31st February on them is quite confusing. What I want to be able to is to have the select boxes only with valid dates. What I mean is that, when you select February, 31st, 30th, (and on some years 29th) are removed, then, when you select January, they're added again, and so forth. Additionally, I want that the initial select boxes are only populated with the days of the selected month.
I've created a date_select in Rails (which is 3 select boxes: one for year, one for month, and one for day).
To have 31st February on them is quite confusing. What I want to be able to is to have the select boxes only with valid dates. What I mean is that, when you select February, 31st, 30th, (and on some years 29th) are removed, then, when you select January, they're added again, and so forth. Additionally, I want that the initial select boxes are only populated with the days of the selected month.
Share Improve this question edited Jun 20, 2018 at 10:14 Zoe - Save the data dump 28.3k22 gold badges128 silver badges160 bronze badges asked Jan 27, 2011 at 22:15 Abel ToyAbel Toy 3285 silver badges16 bronze badges 1- I'd put a bit of jquery there. – apneadiving Commented Jan 27, 2011 at 22:36
2 Answers
Reset to default 10I assume you have three selects with classes 'day', 'month', 'year'
.
Then use this JS:
function monthChanged() {
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var month = $('.month').val() - 1, year = +$('.year').val();
var diff;
// Check for leap year if Feb
if (month == 1 && new Date(year, month, 29).getMonth() == 1) days[1]++;
// Add/Remove options
diff = $('.day option').length - (days[month] + 1);
if (diff > 0) { // Remove
$('.day option').slice(days[month] + 1).remove();
} else if (diff < 0) { // Add
for (var i = $('.day option').length; i <= days[month]; i++) {
$('<option>').attr('value', i).text(i).appendTo('.day');
}
}
}
$(function () {
monthChanged(); // On document ready
$('.month').change(monthChanged); // On month change
$('.year').change(monthChanged); // On year change (for leap years)
});
Demo: http://jsfiddle/FxBpB/
/* automatically update days based on month */
for (var i = 2014; i < 2019; i++) {
$('<option>').attr('value', i).text(i).appendTo('#start_year');
}
function monthChanged() {
debugger;
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var month = $('#start_month').val() - 1,
year = +$('#start_year').val();
// Check for leap year if Feb
if (month == 1 && new Date(year, month, 29).getMonth() == 1)
days[1]++;
// Add/Remove options
if ($('#start_day option').length > days[month]) {
// Remove
$('#start_day option').slice(days[month] + 1).remove();
} else if ($('#start_day option').length < days[month] + 1) {
// Add
for (var i = $('#start_day option').length + 1; i <= days[month]; i++) {
$('<option>').attr('value', i).text(i).appendTo('#start_day');
}
}
}
monthChanged(); // On document ready
$('#start_month').change(monthChanged); // On month change
$('#start_year').change(monthChanged); // On year change (for leap years)
Refer fiddle link: http://jsfiddle/u3fr1mt0/
本文标签: javascriptRepopulating dates on select boxesStack Overflow
版权声明:本文标题:javascript - Repopulating dates on select boxes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741887935a2403132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论