【问题标题】:Minimum Maximum Month of datepicker日期选择器的最小最大月份
【发布时间】:2015-03-28 17:43:30
【问题描述】:

我有两个使用 jquery ui datepicker 的月份和年份选择器我想限制第二个月和年份选择器将仅在第一个日期选择器的月份和年份之后选择我不知道这是否可能请给我如果可能的话,指导我的脚本如下

<script>
$(document).ready(
    function () {
      $( "#date1_in_id" ).datepicker({
        changeMonth: true,//this option for allowing user to select month
        changeYear: true, //this option for allowing user to select from year range
        dateFormat: "mm-yy",
        showButtonPanel: true,
        onClose: function(dateText, inst) { 
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).datepicker('setDate', new Date(year, month, 1));
       }

      });
       $( "#date2_in_id" ).datepicker({
         changeMonth: true,//this option for allowing user to select month
        changeYear: true, //this option for allowing user to select from year range
        dateFormat: "mm-yy",           
        showButtonPanel: true,
        onClose: function(dateText, inst) { 
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).datepicker('setDate', new Date(year, month, 1));
       }            
      });

    });

</script>
<style>.ui-datepicker-calendar{display: none;}</style>

【问题讨论】:

    标签: jquery datepicker


    【解决方案1】:

    您可以使用 minDatema​​xDate 设置最小或最大日期。

    更多信息在这里:http://jqueryui.com/datepicker/#min-max

    【讨论】:

    • 我想限制月份和年份
    【解决方案2】:

    当然你可以使用这个脚本,这个人需要选择第一个日期选择器,然后可以在第一个日期之后选择第二个日期选择器。

    把这个第一个 $("#date1_in_id") 的 onClose 替换掉:

         onClose: function( selectedDate ) {
    
            lafecha = selectedDate.split('-');
            fecha_alreves = lafecha[2]+'-'+lafecha[1]+'-'+lafecha[0];
    
    
            fecha = new Date(fecha_alreves);
    
            ayer = new Date(fecha);
            ayer.setTime(ayer.getTime() - 1000*60*60*24);
            dias = ayer.getDate();
            dias += 01;
            dia = ayer.getDay() + 1;
            mes = ayer.getMonth() + 1;
            anno = ayer.getFullYear() + 1;
            nuevafecha = mes+"-"+dias+"-"+anno;
    
            $( "#date2_in_id" ).datepicker( "option", "minDate", selectedDate );
            $( "#date2_in_id" ).datepicker( "option", "maxDate", nuevafecha );
        }
    

    然后替换这个$("#date2_in_id")的onClose

        onClose: function( selectedDate ) {
            $( "#date1_in_id" ).datepicker( "option", "maxDate", selectedDate );
        }
    

    希望对你有用

    【讨论】:

    • 这是为了日期限制,我只想限制月份和年份,就像 01-2015 一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多