Datepicker将今天的日期设置为默认值

时间:2015-05-06 21:25:15

标签: javascript jquery datepicker set default

所以我有一个日期选择器,当我选择一天时,将一周中的某一天插入到另一个输入中。

$( "#datepicker" ).datepicker({
dateFormat: "dd-mm-yy",
onSelect: function(){
        var seldate = $(this).datepicker('getDate');
        var day = seldate.getUTCDay();
        var weekday=new Array();
            weekday[0]="Maandag";
            weekday[1]="Dinsdag";
            weekday[2]="Woensdag";
            weekday[3]="Donderdag";
            weekday[4]="Vrijdag";
            weekday[5]="Zaterdag";
            weekday[6]="Zondag";
        var dayOfWeek = weekday[day];
        $('#DagWeek').val(dayOfWeek);
    }
});

这完全没问题。但我想在选择任何内容之前将今天的日期+日期设为默认值 所以我在约会时这样做了:

var myDate = new Date();
var month = myDate.getMonth() + 1;
var dag = myDate.getDate();
if(dag < 10) dag = "0" + dag;
if (month < 10) month = "0" + month;
var vandaag = dag + '-' + month + '-' + myDate.getFullYear();
$("#datepicker").val(vandaag);

它也有效,但我现在想把今天也定为默认。所以我试图复制相同的代码
onSelect函数一样,但它不起作用。
问题是:你能告诉我如何在onSelect函数之外设置它吗?因为我试过但它没有用。
为了以防万一,这是我尝试过的:

var day = vandaag.getUTCDay();
alert("test? " + day);
var weekday=new Array();
    weekday[0]="Maandag";
    weekday[1]="Dinsdag";
    weekday[2]="Woensdag";
    weekday[3]="Donderdag";
    weekday[4]="Vrijdag";
    weekday[5]="Zaterdag";
    weekday[6]="Zondag";
var dayOfWeek = weekday[day];
$('#DagWeek').val(dayOfWeek);

编辑:这是HTML:

<td colspan="13">    
    <input type="text" id="DagWeek" name="DagNewSchema" size="12" readonly>
    &nbsp;
    <input type="text" id="datepicker" name="NSchemaDatum" size="15" required>
</td>

因此,这是我第一次打开页面时得到的结果:
enter image description here
这就是我从datepicker中选择日期时得到的结果:
enter image description here

1 个答案:

答案 0 :(得分:1)

这样做:

function updateWeekDay($datepicker){
    var seldate = $datepicker.datepicker('getDate');
    var day = seldate.getUTCDay();
    var weekday=new Array();
        weekday[0]="Maandag";
        weekday[1]="Dinsdag";
        weekday[2]="Woensdag";
        weekday[3]="Donderdag";
        weekday[4]="Vrijdag";
        weekday[5]="Zaterdag";
        weekday[6]="Zondag";
    var dayOfWeek = weekday[day];
    $('#DagWeek').val(dayOfWeek);
};

$( "#datepicker" ).datepicker({
dateFormat: "dd-mm-yy",
onSelect: function(){
        updateWeekDay($(this));
    }
});

var myDate = new Date();
var month = myDate.getMonth() + 1;
var dag = myDate.getDate();
if(dag < 10) dag = "0" + dag;
if (month < 10) month = "0" + month;
var vandaag = dag + '-' + month + '-' + myDate.getFullYear();
$("#datepicker").val(vandaag);
updateWeekDay($("#datepicker"));

我可能会遇到一些语法错误,因为我在没有测试的情况下键入了这个错误,但是你明白了,将代码拉入函数调用它。