gpt4 book ai didi

javascript - Datepicker 将今天设置为默认日期

转载 作者:行者123 更新时间:2023-11-30 00:29:42 25 4
gpt4 key购买 nike

所以我有一个日期选择器,当我选择一天时,星期几会插入到另一个输入中。

$( "#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
这就是我从日期选择器中实际选择日期时得到的结果:
enter image description here

最佳答案

这样做:

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"));

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

关于javascript - Datepicker 将今天设置为默认日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com