Datepicker 将今天设置为默认日期
Datepicker set today's day as a default
所以我有一个日期选择器,当我 select 一天时,星期几被插入到另一个输入中。
$( "#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);
}
});
这很好用。但我想在他们select任何事情之前将今天的日期+日期设置为默认值。
所以我为日期做了这个:
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>
<input type="text" id="datepicker" name="NSchemaDatum" size="15" required>
</td>
因此,这是我第一次打开页面时得到的结果:
这就是我实际上 select 来自日期选择器的日期时得到的结果:
这样做:
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"));
我在未经测试的情况下键入此内容时可能会出现一些语法错误,但您明白了,将代码拉入函数并调用它。
所以我有一个日期选择器,当我 select 一天时,星期几被插入到另一个输入中。
$( "#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);
}
});
这很好用。但我想在他们select任何事情之前将今天的日期+日期设置为默认值。
所以我为日期做了这个:
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>
<input type="text" id="datepicker" name="NSchemaDatum" size="15" required>
</td>
因此,这是我第一次打开页面时得到的结果:
这就是我实际上 select 来自日期选择器的日期时得到的结果:
这样做:
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"));
我在未经测试的情况下键入此内容时可能会出现一些语法错误,但您明白了,将代码拉入函数并调用它。