jquery datepicker默认日期 [英] jquery datepicker default date

查看:133
本文介绍了jquery datepicker默认日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个日期选择器的属性以动态拉动日期,但是当我尝试将其设置为一个变量时,我会收到未捕获的异常错误。

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.

错误只发生在没有日历(内联)的页面上。
如何从选择器中删除rel标签,而不会收到此错误?

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?

//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;
    }});}});
}

更新
正确,线是我遇到的问题,
从里面的#calendar-inline中拉取属性rel的正确方法是什么。
所有尝试都在js中抛出一个未捕获的错误

UPDATE Correct, 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

更新2

function calendar_picker () {
var myDate = new Date($("#calendar-inline").attr('rel'));
    $("#calendar-inline").datepicker({

     dateformat: 'yy-mm-dd',
     defaultDate:myDate,

解决方案:

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,


推荐答案

尝试这样:

defaultDate: $("#calendar-inline").attr('rel')

这将尝试拉来自'rel'属性的日期,如果不存在,它应该返回null。当null被传入datepicker默认日期时,它将使用今天作为默认日期。

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.

这篇关于jquery datepicker默认日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆