简体   繁体   English

尝试在两个输入的 jquery datepicker 中设置最小和最大时间日期

[英]Trying to set min & max time date in jquery datepicker in two inputs

So I am working on a project for my classes and I had to set minimum and maximum date for two inputs.所以我正在为我的课程做一个项目,我必须为两个输入设置最小和最大日期。 First one is being set from now to 12 months from now, and the other was meant to be set from date which was picked in the first input field to 12 months from now.第一个设置是从现在到 12 个月,另一个是从第一个输入字段中选择的日期设置到 12 个月。

<p>From: <input type="text" id="dateFrom"></p>
<p>To: <input type="text" id="dateTo"></p>

this is my JS这是我的 JS

    var chosenDate = document.getElementById("dateFrom").value;
$( function() {
          $("#dateFrom").datepicker({
          dateFormat: "dd-mm-yy",
          changeMonth: true,
          changeYear: true,
          maxDate: '12M',
          minDate: '0',
            });
        })

      $( function() {
          $("#dateTo").datepicker({
          dateFormat: "dd-mm-yy",
          changeMonth: true,
          changeYear: true,
          maxDate: '12M',
          minDate: chosenDate,
            });
        })

On the preview in a browser it works only for last session.在浏览器的预览中,它仅适用于上次会话。 How can I fix this?我怎样才能解决这个问题?

you have to use change event on #dateFrom, so whenever the value of the input is changed,the minDate property of the datepicker event get the current input value of #dateFrom你必须在#dateFrom 上使用change 事件,所以每当输入的值改变时,datepicker 事件的minDate 属性获取#dateFrom 的当前输入值

$("#dateFrom").change(() => {
$("#dateTo").datepicker("option", "minDate", chosenDate.value);
});

the final code should be like this:最终的代码应该是这样的:

var chosenDate = document.getElementById("dateFrom");
$(function() {
  $("#dateFrom").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    maxDate: "12M",
    minDate: "0"
  });
});
$(function() {
  $("#dateTo").datepicker({
    dateFormat: "dd-mm-yy",
    changeMonth: true,
    changeYear: true,
    maxDate: "12M",
    minDate: chosenDate.value
  });
  $("#dateFrom").change(() => {
    $("#dateTo").datepicker("option", "minDate", chosenDate.value);
  });
});

notice the chosenDate variable is now just the element not the value of the element注意 selectedDate 变量现在只是元素而不是元素的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM