编辑:我正在使用这个日期选择器BTW: https://github.com/CuriousSolutions/DateTimePicker
我在HTML页面上执行此操作,它可以正常工作:
$(document).ready(function() {
var now = new Date();
now = new Date();
now.addWeeks(0);
$("#dt0weeks")[0].valueAsDate = now;
now = new Date();
now.addWeeks(18);
$("#dt18weeks")[0].valueAsDate = now;
now = new Date();
now.addWeeks(90);
$("#dt90weeks")[0].valueAsDate = now;
now = new Date();
now.addWeeks(1);
$("#dt1weeks")[0].valueAsDate = now;
});
它可以正常工作。
但是,当我尝试做几乎相同的事情时:
$(function() {
$("#dt0weeks").change(function () {
alert('I changed on you');
//now = new Date();
//now.addWeeks(0);
//$("#dt0weeks")[0].valueAsDate = now;
var now = new Date();
now = new Date("2019-03-25");
now.addWeeks(18);
$("#dt18weeks")[0].valueAsDate = now;
now = new Date("2015-03-25");
now.addWeeks(90);
$("#dt90weeks")[0].valueAsDate = now;
now = new Date("2011-03-25");
now.addWeeks(1);
$("#dt1weeks")[0].valueAsDate = now;
});
});
它不会工作,我收到Failed to set the 'valueAsDate' property on 'HTMLInputElement'
错误,这是一张图片。我不明白为什么。