Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery UI日期选择器和选择框_Jquery_Jquery Ui_Datepicker_Uidatepicker - Fatal编程技术网

jQuery UI日期选择器和选择框

jQuery UI日期选择器和选择框,jquery,jquery-ui,datepicker,uidatepicker,Jquery,Jquery Ui,Datepicker,Uidatepicker,我在jQueryUI中试用了datepicker,可以使用选择框,但只有在有日、月和年选择框的情况下才能使用 我需要的是有一个像在这里可以看到的那样的一年一天一个月的盒子 有人知道这是否可以做到吗?完全撞到了砖墙。基本上,您需要从 更改此部分,日期选择器的初始化 $(function() { // initialise the "Select date" link $('#date-pick') .datePicker( // assoc

我在jQueryUI中试用了datepicker,可以使用选择框,但只有在有日、月和年选择框的情况下才能使用

我需要的是有一个像在这里可以看到的那样的一年一天一个月的盒子


有人知道这是否可以做到吗?完全撞到了砖墙。

基本上,您需要从

更改此部分,日期选择器的初始化

$(function()
{

    // initialise the "Select date" link
    $('#date-pick')
        .datePicker(
            // associate the link with a date picker
            {
                createButton:false,
                startDate:'01/01/2005',
                endDate:'31/12/2010'
            }
为此,请添加月数组并更新endDate

$(function()
{
    var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

    // initialise the "Select date" link
    $('#date-pick')
        .datePicker(
            // associate the link with a date picker
            {
                createButton:false,
                startDate:'01/01/2005',
                endDate:'31/12/2015'
            }
然后,更改更新3个选项d、m、y的函数

var updateSelects = function (selectedDate)
{
    var selectedDate = new Date(selectedDate);
    $('#d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
    $('#m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
    $('#y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
}
仅为d,m,该格式中指定的月份为2011年8月

var updateSelects = function (selectedDate)
{
    var selectedDate = new Date(selectedDate);
    $('#d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
    $('#m option[value=' + (months[selectedDate.getMonth()]) + ' ' +  (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
}
然后,更改在更改此选项时更新选择器的函数

// listen for when the selects are changed and update the picker
$('#d, #m, #y')
    .bind(
        'change',
        function()
        {
            var d = new Date(
                        $('#y').val(),
                        $('#m').val()-1,
                        $('#d').val()
                    );
            $('#date-pick').dpSetSelected(d.asString());
        }
    );
编辑:我在上一个答案中错误地交换了年值和日值

// listen for when the selects are changed and update the picker
$('#d, #m')
    .bind(
        'change',
        function()
        {
            var d = new Date(
                        $('#m').val().split(" ")[1],
                        months.indexOf($('#m').val().split(" ")[0]),
                        $('#d').val()
                    );
            $('#date-pick').dpSetSelected(d.asString());
        }
    );
这是一个能做到这一点的方法。
希望这能解决您的问题。

将您已经完成的工作张贴在网站上,并分享链接。这样,每个人都可以更好地帮助您。在选择日期时,您不能只使用onselect处理程序来适当地设置下拉元素吗。。。我尝试了下面ace的解决方案,但没有成功。谢谢ace,但它不起作用。在上面的第一篇文章中,我添加了一个指向JSFIDLE的链接。是的,当我检查它时,它也不起作用。设置为最近日期时,代码似乎有问题。我会在一段时间内更新我的答案。这是一个工作代码。我正在计算他的代码的错误,注意在他的示例中没有2011年,尝试添加2011年和2012年,他的代码将不起作用。我的错。有一个endDate配置。我会在一段时间内更新我的答案。谢谢你的帮助。我需要删除年份选择框,并将月份和年份放在一个选择框中,如2011年8月、2012年10月等