jquery datepicker 默认日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1646590/
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
jquery datepicker default date
提问by matthewb
I am trying to get a attribute of a date picker to pull the date dynamically, however I get uncaught exception errors when I try to set it to a variable.
我正在尝试获取日期选择器的属性以动态提取日期,但是当我尝试将其设置为变量时出现未捕获的异常错误。
The errors only occur on pages that do NOT have the calendar (inline). How can I pul the rel tag from the selector without getting this error?
错误仅发生在没有日历(内联)的页面上。如何从选择器中提取 rel 标签而不会出现此错误?
//Event Calendar Home Page and Listing
function calendar_picker () {
$("#calendar-inline").datepicker({
//defaultDate: $(this).attr('rel'),
dateformat: 'yy-mm-dd',
maxDate: '+1y',
minDate:'-0d',
hideIfNoPrevNext: true,
showButtonPanel: false,
navigationAsDateFormat: false,
onSelect: function(dateText, inst) {
var d = new Date(dateText);
var fmt1 = $.datepicker.formatDate("yy-mm-dd", d);
$.ajax({
type: "POST",
url: "/events/listing/all/20/",
dataType: "html",
date: "event_date="+fmt1,
success: function(){
window.location.href= "/events/browse/"+fmt1;
}});}});
}
UPDATECorrect, the commented line is what I am having issues with, What is the correct way to pull the attribute rel from #calendar-inline from inside this. All attempts throw a uncaught error in js
更新正确,注释行是我遇到的问题,从#calendar-inline 中提取属性 rel 的正确方法是什么。所有尝试都在 js 中抛出一个未捕获的错误
Update 2
更新 2
function calendar_picker () {
var myDate = new Date($("#calendar-inline").attr('rel'));
$("#calendar-inline").datepicker({
dateformat: 'yy-mm-dd',
defaultDate:myDate,
Solution:
解决方案:
function calendar_picker () {
var myDate = null;
if ($("#calendar-inline").attr('rel') != null) {
myDate = $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel'));
}
$("#calendar-inline").datepicker({
dateformat: 'yy-mm-dd',
defaultDate:myDate,
采纳答案by David Radcliffe
try this:
尝试这个:
defaultDate: $("#calendar-inline").attr('rel')
This will attempt to pull the date from the 'rel' attribute and if that doesn't exist it should return null. When null is passed into the datepicker default date it will use today as the default date.
这将尝试从 'rel' 属性中提取日期,如果该属性不存在,则应返回 null。当 null 传递到 datepicker 默认日期时,它将使用今天作为默认日期。
回答by Somnath Muluk
We can set default date by this:
我们可以通过以下方式设置默认日期:
<script type="text/javascript">
$(function() {
$("#date" ).datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1920:2010',
dateFormat : 'dd-mm-yy',
defaultDate: new Date(2000,01,01)
});
});
</script>
See http://jqueryui.com/demos/datepicker/#option-defaultDate
回答by Joshua Kissoon
this works for me:
这对我有用:
var nowDate = new Date(year, month, day, hours, minutes, seconds, milliseconds);
then in the date picker, you set:
然后在日期选择器中,您设置:
defaultDate: nowDate;
回答by Whisk
Your question is a little unclear - what I'm assuming is that your problem occurs on the commented out line.
您的问题有点不清楚 - 我假设您的问题发生在注释掉的行上。
If that's correct then I think your problem is with the "this". I'm not quite sure what you're expecting "this" to be, but inside the function it's going to be the window object.
如果那是正确的,那么我认为您的问题出在“this”上。我不太确定您期望“this”是什么,但在函数内部它将是 window 对象。
If I'm on completely the wrong track please update the question with some more information.
如果我完全走错了路,请用更多信息更新问题。