Jquery: DatePicker: start/end date

前端 未结 3 626
太阳男子
太阳男子 2020-12-12 01:29

i have looked around before posting my question

following what i am looking in my datepicker (start date and end date):

1) Start date: can b

相关标签:
3条回答
  • 2020-12-12 01:52
    <input type="text" id="tbStartDate" value="" disabled="disabled" />
    <input type="text" id="tbEndDate" value="" disabled="disabled" />
    <script type="text/javascript">
        $(document).ready(function () {
            $("#tbStartDate").datepicker({        
                showOn: 'button',
                buttonImageOnly: true,
                buttonImage: '/Content/Calendar.png',
                buttonText: 'Click here (date)',
                onSelect: function (dateText, inst) {
                     var $endDate = $('#tbStartDate').datepicker('getDate');
                     $endDate.setDate($endDate.getDate() + 1);
                     $('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
                },
                onClose: function (dateText, inst) {
                    //$("#StartDate").val($("#tbStartDate").val());
                }
            });
    
            $("#tbEndDate").datepicker({
                dateFormat: 'dd-mm-yy',
                showOn: 'button',
                buttonImageOnly: true,
                buttonImage: '/Content/Calendar.png',
                buttonText: 'Click here (date)',
                onClose: function (dateText, inst) {
                    //$("#EndDate").val($("#tbEndDate").val());
                }
            });
    
                var $endDate = $('#tbStartDate').datepicker('getDate');
                $endDate.setDate($endDate.getDate() + 1);
                $('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
        });
    </script>
    
    0 讨论(0)
  • 2020-12-12 02:01

    I answered a similar question for a friend. He needed a cascading date similar, but I think you can see how to solve the issue if you look at my markup.

    HTML

      <form target="_self" action="ClearForm.aspx">
    

    Clear Form

    • Start Date:
    • End Date:
    • Clear

    JS

    <script type="text/javascript">
    $(document).ready(function () {
    
      $('#endDate').datepicker({ showOn: 'button',
          buttonImage: '../images/Calendar.png',
          buttonImageOnly: true, onSelect: function () { },
          onClose: function () { $(this).focus(); }
        });
    
    
      $('#startDate').datepicker({ showOn: 'button',
          buttonImage: '../images/Calendar.png',
          buttonImageOnly: true, onSelect:
            function (dateText, inst) {
              $('#endDate').datepicker("option", 'minDate', new Date(dateText));
            }
          ,
          onClose: function () { $(this).focus(); }
        });
    
    
    });             
    

    0 讨论(0)
  • 2020-12-12 02:06

    I prefer to write a default configuration for the datepicker using a class so that it works with more than one with less duplicates.

    onSelect: function(dateText, inst){
        // Check if _until exists and auto set mindate
        if(inst.id.contains("_from")){
            $("#"+inst.id.replace("_from", "_until")).datepicker("option", "minDate", dateText);
        }
    }
    

    This works assuming your form IDs are consistent. E.g I use date_until and date_from

    0 讨论(0)
提交回复
热议问题