是否有 jquery 下拉年份选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5620996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is there a jquery dropdown year selector
提问by Nicola Peluchetti
is there a jQuery plugin that creates automatically a dropdown year selector (that is a "select" element poulated with all years starting from current and dating back to a given year)?
是否有一个 jQuery 插件可以自动创建一个下拉年份选择器(这是一个“选择”元素,填充从当前开始并追溯到给定年份的所有年份)?
I don't need day/month (otherwise i'd have used the datepicker), i just need the year!
我不需要日/月(否则我会使用日期选择器),我只需要年份!
Thanks in advance
提前致谢
回答by Jimmy Sawczuk
What about just:
怎么样:
<select name="yearpicker" id="yearpicker"></select>
And then:
进而:
for (i = new Date().getFullYear(); i > 1900; i--)
{
$('#yearpicker').append($('<option />').val(i).html(i));
}
回答by VLADISLAV BOGOMOLNY
Wrapping around the code into jQuery plugin to select years between start and end year:
将代码包装到 jQuery 插件中以选择开始和结束年份之间的年份:
jQuery.extend(jQuery.fn,{
/*
** Year selection plugin
** Sample usage
** $('#yearpicker').years(2007, 2017);
**
** HTML
** <select name="yearpicker" id="yearpicker"></select>
*/
years: function(start, end) {
for (i = start; i <= end; i++)
{
$(this).append($('<option />').val(i).html(i));
}
}
});
回答by Martin Sansone - MiOEE
Expanding on Jimmy Sawczuk's solution - what if you also needed to process feedback data into a form page where a previous selection needed to be loaded/selected.
扩展 Jimmy Sawczuk 的解决方案 - 如果您还需要将反馈数据处理到需要加载/选择先前选择的表单页面中,该怎么办。
A variable holding the user's previous selection can be used in an if statement to dynamically assign the "Selected" attribute like so:
可以在 if 语句中使用保存用户先前选择的变量来动态分配“Selected”属性,如下所示:
for (i = new Date().getFullYear(); i > 1900; i--)
{
if(userselection == i){
$('#yearpicker').append($('<option />').val(i).attr("selected","selected").html(i));
} else {
$('#yearpicker').append($('<option />').val(i).html(i));
}
}
回答by mmv_sat
datepicker has the minViewMode option. In my case I used it for date ranges in years:
datepicker 有 minViewMode 选项。就我而言,我将它用于以年为单位的日期范围:
JavaScript: $('.input-daterange').datepicker({ minViewMode: 2, format: 'yyyy' });
JavaScript: $('.input-daterange').datepicker({ minViewMode: 2, format: 'yyyy' });
HTML:
HTML:
<div class="input-daterange date"id="DataRange">
<input type="text"class="input-small"/>
<span class="add-on">to</span>
<input type="text"class="input-small"/>
</div>