.datepicker('setdate') 问题,在 jQuery 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1953840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:27:36  来源:igfitidea点击:

.datepicker('setdate') issues, in jQuery

jqueryjquery-uijquery-ui-datepicker

提问by Derek Adair

I have a function that executes a query to get some data based on a certain date range which is selected using .datepicker(). I am trying to set the datepicker's that are in the response data back to the date range which was selected before the query was executed.

我有一个函数,它执行查询以根据使用 .datepicker() 选择的特定日期范围获取一些数据。我试图将响应数据中的日期选择器设置回执行查询之前选择的日期范围。

queryDate = '2009-11-01';
$('#datePicker').datepicker('setDate', queryDate);

has the interesting result of setting the datepicker to today's date! I wouldn't be so confused if it just spit out an error. Why does it set it to today's date?

将日期选择器设置为今天的日期有一个有趣的结果!如果它只是吐出一个错误,我就不会那么困惑。为什么将其设置为今天的日期?

How can I take the date which is formated like, 'yyyy-mm-dd' and set the datepicker to it?

如何获取格式为“yyyy-mm-dd”的日期并将日期选择器设置为它?

I am thinking it might be best to just set the text-box which the datepicker is linked to to 'queryDate'.

我认为最好只设置日期选择器链接到“queryDate”的文本框。

采纳答案by CMS

If you would like to support really old browsers you should parse the date string, since using the ISO8601 date format with the Dateconstructor is not supported pre IE9:

如果您想支持非常旧的浏览器,您应该解析日期字符串,因为在DateIE9 之前不支持使用 ISO8601 日期格式和构造函数:

var queryDate = '2009-11-01',
    dateParts = queryDate.match(/(\d+)/g)
    realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);  
                                    // months are 0-based!
// For >= IE9
var realDate = new Date('2009-11-01');  

$('#datePicker').datepicker({ dateFormat: 'yy-mm-dd' }); // format to show
$('#datePicker').datepicker('setDate', realDate);

Check the above example here.

这里检查上面的例子。

回答by jurka

When you trying to call setDate you must provide valid javascript Date object.

当您尝试调用 setDate 时,您必须提供有效的 javascript Date 对象。

queryDate = '2009-11-01';

var parsedDate = $.datepicker.parseDate('yy-mm-dd', queryDate);

$('#datePicker').datepicker('setDate', parsedDate);

This will allow you to use different formats for query date and string date representation in datepicker. This approach is very helpful when you create multilingual site. Another helpful function is formatDate, which formats javascript date object to string.

这将允许您在 datepicker 中使用不同格式的查询日期和字符串日期表示。当您创建多语言站点时,这种方法非常有用。另一个有用的函数是 formatDate,它将 javascript 日期对象格式化为字符串。

$.datepicker.formatDate( format, date, settings );

回答by Matt Ball

As Scobal's post implies, the datepicker is looking for a Date object - not just a string! So, to modify your example code to do what you want:

正如 Scobal 的帖子所暗示的那样,日期选择器正在寻找一个 Date 对象——而不仅仅是一个字符串!因此,要修改您的示例代码以执行您想要的操作:

var queryDate = new Date('2009/11/01'); // Dashes won't work
$('#datePicker').datepicker('setDate', queryDate);

回答by Mark Pope

Try changing it to:

尝试将其更改为:

queryDate = '2009-11-01';
$('#datePicker').datepicker({defaultDate: new Date (queryDate)});

回答by jomon

function calenderEdit(dob) {
    var date= $('#'+dob).val();
    $("#dob").datepicker({
        changeMonth: true,
        changeYear: true, yearRange: '1950:+10'
    }).datepicker("setDate", date);
}

回答by cfwall

Check that the date you are trying to set it to lies within the allowed date range if the minDate or maxDate options are set.

如果设置了 minDate 或 maxDate 选项,请检查您尝试设置的日期是否在允许的日期范围内。