我有一个非常简单的jQuery Datepicker日历:

$(document).ready(function(){
    $("#date_pretty").datepicker({ 
    });
});

当然在HTML中…

<input type="text" size="10" value="" id="date_pretty"/>

今天的日期是很好的突出显示为用户时,他们带来的日历,但我如何让jQuery预填充文本框本身与今天的日期在页面加载,而不需要用户做任何事情?99%的情况下,今天的日期默认是他们想要的。


当前回答

var myDate = new Date();
var prettyDate =(myDate.getMonth()+1) + '/' + myDate.getDate() + '/' +
        myDate.getFullYear();
$("#date_pretty").val(prettyDate);

似乎很管用,但也许还有更好的办法。

其他回答

var myDate = new Date();
var prettyDate =(myDate.getMonth()+1) + '/' + myDate.getDate() + '/' +
        myDate.getFullYear();
$("#date_pretty").val(prettyDate);

似乎很管用,但也许还有更好的办法。

David K Egghead的代码工作得很完美,谢谢!

$(".date-pick").datepicker(); 
$(".date-pick").datepicker("setDate", new Date()); 

我也设法修剪了一下,它也有用:

$(".date-pick").datepicker().datepicker("setDate", new Date()); 

解决方案是:

$(document).ready(function(){
    $("#date_pretty").datepicker({ 
    });
    var myDate = new Date();
    var month = myDate.getMonth() + 1;
    var prettyDate = month + '/' + myDate.getDate() + '/' + myDate.getFullYear();
    $("#date_pretty").val(prettyDate);
});

谢谢grayghost !

更新:有报道称,这将不再适用于Chrome浏览器。

这句话简明扼要,能起到作用(过时):

$(".date-pick").datepicker('setDate', new Date());

这是不那么简洁的,利用链接允许它在chrome中工作(2019-06-04):

$(".date-pick").datepicker().datepicker('setDate', new Date());

你有两个选择:

选项A)在日历中标记为“活动”,仅当您单击输入时。

Js:

$('input.datepicker').datepicker(
                        {   
                            changeMonth: false,
                            changeYear: false,
                            beforeShow: function(input, instance) { 
                                $(input).datepicker('setDate', new Date());
                            }
                        }                         
                     );

Css:

div.ui-datepicker table.ui-datepicker-calendar .ui-state-active,
        div.ui-datepicker table.ui-datepicker-calendar .ui-widget-content .ui-state-active  {
            background: #1ABC9C;
            border-radius: 50%;
            color: #fff;
            cursor: pointer;
            display: inline-block;
            width: 24px; height: 24px;
        }​

选项B)默认输入今天。 您必须首先填充datepicker。

$("input.datepicker").datepicker().datepicker("setDate", new Date());