Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery defaultDate选项为空或缺失时的租金日期(这是问题的核心)_Jquery_Jquery Ui_Datepicker - Fatal编程技术网

Jquery defaultDate选项为空或缺失时的租金日期(这是问题的核心)

Jquery defaultDate选项为空或缺失时的租金日期(这是问题的核心),jquery,jquery-ui,datepicker,Jquery,Jquery Ui,Datepicker,我改变了这个方法 _getDefaultDate: function(inst) { return this._restrictMinMax(inst,this._determineDate(inst,this._get(inst, "defaultDate"), new Date())); } 对此 _getDefaultDate: function(inst) { var def = this._get(inst, "defaultDate");

我改变了这个方法

    _getDefaultDate: function(inst) {
        return this._restrictMinMax(inst,this._determineDate(inst,this._get(inst, "defaultDate"), new Date()));
    }
对此

_getDefaultDate: function(inst) {
    var def = this._get(inst, "defaultDate");
    return def !== null ? this._restrictMinMax(inst, this._determineDate(inst, def , new Date())): null;
},
  • 方法_setDate将所选日期设置为日期参数中传递null时的当前日期。我更改为lines以更正此问题:
  • 我改了这行

    inst.selectedDay = inst.currentDay = newDate.getDate();
    
    this._adjustInstDate(inst);
    
        (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime()) ?
    

    这条线

    inst.selectedDay = inst.currentDay = newDate.getDate();
    
    this._adjustInstDate(inst);
    
        (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime()) ?
    

  • 最后,方法_generateHTML没有处理来自_getDefaultDate的null返回值 所以我改了这句话

    inst.selectedDay = inst.currentDay = newDate.getDate();
    
    this._adjustInstDate(inst);
    
        (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime()) ?
    

    就这样。现在,Datepicker defaultDate选项更直观地响应传入的null值或缺少此选项

    $('#test').datepicker({ altField: '#alt'});  // No date selected
    $('#test').datepicker({ altField: '#alt', defaultDate: null});  // No date selected
    $('#test').datepicker({ altField: '#alt', defaultDate: 0});  // Today selected
    $('#test').datepicker({ altField: '#alt', defaultDate: +1});  // Tomorrow selected
    $('#test').datepicker({ altField: '#alt', defaultDate: -1});  // Yesterday selected
    
    下面是JSFIDLE中的一个演示:

    请注意:这些更改还没有经过详尽的测试,因此可能会出现其他问题,因为_getDefaultDate方法现在可能返回null。所以先做你自己的测试!;)


    希望这对别人有帮助!:)

    我的解决方案涉及覆盖jQuery的append,这样我们可以在每次重新生成HTML并将其附加到div时删除元素。这修复了切换月份时默认日期的闪烁,并清除了悬停

    (function($) {
        var origAppend = $.fn.append;
    
        $.fn.append = function () {
            return origAppend.apply(this, arguments).trigger("append");
        };
    })(jQuery);
    
    var datePickerDiv = $("#date-picker");
    datePickerDiv.bind("append", function() {
      $(this).find(".ui-datepicker-days-cell-over").removeClass("ui-datepicker-days-cell-over");
      $(this).find(".ui-datepicker-today a ").removeClass("ui-state-highlight ui-state-active ui-state-hover");
    });
    
    datePickerDiv.datepicker();
    
    为我工作:

    $(document).ready(function() {
        $("#datepicker .ui-state-active").removeClass("ui-state-active");
        $("#datepicker .ui-state-highlight").removeClass("ui-state-highlight");
        $("#datepicker .ui-datepicker-today").removeClass("ui-datepicker-today");
    });
    

    也可以按如下方式设置空值:

      $(this).datepicker({defaultDate: null});
    


    当然,我的意思是,当日期选择器打开时,我不想要一个选定的日期。在我的网站中,我想强制用户在继续之前做出选择。如果设置正确,它将打开,并且没有选择日期。user352985-查看我发布的答案,这是可以做到的,实际上也很容易做到。我对此投了反对票,因为似乎有一个自动的假设,即今天的日期应该默认选择。如果您使用日历作为任何类型预订系统的基础,这是非常没有帮助的-我的客户需要能够选择自己的日期。datepicker功能基本上已损坏。如果该日期在内部未设置(使用API中记录的批准方法),则该日期之后不应在日历上突出显示。没有理由在默认情况下在日历上选择任何日期,这只是它的配置方式。@NoelWhitemore我所说的“您必须有一个默认日期”是指在打开选择器时需要显示一些内容,对吗?如果没有默认日期,您将显示哪个月/年?如果没有默认日期,您如何决定显示什么?也许它不应该被选中,但只要决定显示哪个月/年,但你仍然需要一些开始!好的,我现在明白你的意思了:)可以将日历视图设置为当前月份(大多数用户都会这样认为),但我认为自动选择今天的日期是不合适的,就像用户自己已经点击了日历一样。datepicker小部件的问题是,当我使用API取消选择默认日期时,即使(内部)日期未设置,日历上仍会突出显示该日期。解决这一问题的唯一方法似乎是手动删除CSS样式或破解jQuery UI代码。我确实做到了。你在删除之前读过我的评论吗?是吗?我没有@Aaronmileri认为你有,但很公平;要点是强制使用默认值会使表单验证无法确定进行,而更好的方法是在datepicker API中分别公开“显示本月和本年”和“设置或清除默认值”功能。问题在于重画日历时。例如,转到下一个月,然后返回到当前月份,“今天”亮起,就好像它已被选中一样。(对不起,我没有办法解决这个问题。)好吧,你也许可以做一些欺骗来让它起作用。考虑到我的用例,我可以简单地检查continue按钮是否可见(即用户选择了日期)。如果不是,我知道它仍然是默认选择的,可以再次取消选择。我不确定我将如何钩住月份切换代码(可能是在点击下一个/上一个按钮时),但我不认为这是不可能的。是的,我在发布上述内容后不久就找到了
    onChangeMonthYear
    函数,并使用它:
    onChangeMonthYear:function(year,month,inst){if(还没有选择)setTimeout(function(){$(“#datepicker.ui state active”).removeClass(“ui state active”)},1)}
    (奇怪的是,我没有任何注释来解释为什么我将它包装在
    setTimeout
    ;但是,即使不是必需的,它也没有什么坏处。)Darren cook>像onSelect和onChangeMonthYear这样的触发器在事件发生之前会在ShowDay之前触发。因此,如果不使用setTimeout,您可以在它实际绘制之前将其删除。这是我的工作程序,而不是第二行:
    $('.calendar')。查找(“.ui datepicker current day a”)。removeClass(“ui状态突出显示ui状态活动”)
    谢谢你的代码!我还必须在
    day=Math.min(inst.selectedDay,this.\u getDaysInMonth(year,month))+(period==“D”?偏移量:0)之后添加
    day=day |,
    \u adjustInstDate
    功能中,单击箭头更改月份将在第一次单击时起作用。否则,第一次单击不会更改月份。欢迎您,很高兴它有所帮助!也感谢您的更新。通过删除
    用户界面状态活动
    用户界面状态悬停
    f,我获得了更好的结果rom子元素。
    $('.ui-datepicker current day.ui-state-active')。removeClass('ui-state-active')。removeClass('ui-state-hover');
    可能是因为版本更改。如果我们可以在onClose中设置altField的日期值,如果输入字段为空,那就太棒了。只是在原始问题之外做梦。。。
    _getDefaultDate: function(inst) {
        var def = this._get(inst, "defaultDate");
        return def !== null ? this._restrictMinMax(inst, this._determineDate(inst, def , new Date())): null;
    },
    
    inst.selectedDay = inst.currentDay = newDate.getDate();
    
    inst.selectedDay = date == null? 0: inst.currentDay = newDate.getDate();
    
    this._adjustInstDate(inst);
    
    if (date != null) {
        this._adjustInstDate(inst);
    }
    
        (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime()) ?
    
    (defaultDate != null && (defaultDate.getTime() === printDate.getTime() && defaultDate.getTime() === selectedDate.getTime())) ?
    
    $('#test').datepicker({ altField: '#alt'});  // No date selected
    $('#test').datepicker({ altField: '#alt', defaultDate: null});  // No date selected
    $('#test').datepicker({ altField: '#alt', defaultDate: 0});  // Today selected
    $('#test').datepicker({ altField: '#alt', defaultDate: +1});  // Tomorrow selected
    $('#test').datepicker({ altField: '#alt', defaultDate: -1});  // Yesterday selected
    
    (function($) {
        var origAppend = $.fn.append;
    
        $.fn.append = function () {
            return origAppend.apply(this, arguments).trigger("append");
        };
    })(jQuery);
    
    var datePickerDiv = $("#date-picker");
    datePickerDiv.bind("append", function() {
      $(this).find(".ui-datepicker-days-cell-over").removeClass("ui-datepicker-days-cell-over");
      $(this).find(".ui-datepicker-today a ").removeClass("ui-state-highlight ui-state-active ui-state-hover");
    });
    
    datePickerDiv.datepicker();
    
    $(document).ready(function() {
        $("#datepicker .ui-state-active").removeClass("ui-state-active");
        $("#datepicker .ui-state-highlight").removeClass("ui-state-highlight");
        $("#datepicker .ui-datepicker-today").removeClass("ui-datepicker-today");
    });
    
      $(this).datepicker({defaultDate: null});