简体   繁体   English

如何在 jquery ui 选择输入中设置日期

[英]How to set date in jquery ui select inputs

Good evening everyone, I created a JQuery Ui datepicker for months and years, here's the code:大家晚上好,我创建了一个 JQuery Ui datepicker 几个月和几年,这里是代码:

  $.datepicker.setDefaults($.datepicker.regional["pt-BR"]);
  $(datePickerId).datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel:true,
    maxDate: "+3m",
    minDate: "-2y",
    onClose: function(dateText, inst) {
      $(this).prop('disabled', true);
      let month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
      let year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
      let givenDate = new Date(year, month, 1);
      $(this).datepicker('setDate', givenDate);
      //Do something with AJAX
    }
  });
  $(datePickerId).datepicker("setDate", "0")

My question is, when the user will select the date the input is something like this:我的问题是,当用户选择日期时,输入是这样的:

And this image is the perfect example, in this case the user had selected the month of August, but as soon as he clicked on the datepicker again the datepicker picked up the default date that is set to bring when it runs for the first time (todays month/year), when the user selects a new month/year how could I make the code "remember" which was selected in the onClose method and then bring it correctly?这张图片是一个完美的例子,在这种情况下,用户选择了八月,但是一旦他再次点击日期选择器,日期选择器就会选择第一次运行时设置的默认日期(今天的月/年),当用户选择新的月/年时,我怎样才能让代码“记住”在 onClose 方法中选择的代码,然后正确地输入? Thank you very much in advance.非常感谢您提前。

Edit to fully describe what I actually want to do: User opens the calendar, it starts with the actual month and year, user chooses July/2019, clicks ok, an AJAX request is done to the server to pick some information for the given month, user clicks datepicker again, the calendar shows up, when this happens the inputs from month and year (see image for reference) should be at July/2019 (the last month/year user choosed).编辑以完全描述我真正想做的事情:用户打开日历,从实际的月份和年份开始,用户选择 2019 年 7 月,单击确定,向服务器发送 AJAX 请求以选择给定月份的一些信息,用户再次单击日期选择器,日历显示,当发生这种情况时,月份和年份的输入(参见图片供参考)应该在 2019 年 7 月(用户选择的最后一个月/年)。

Consider the following example.考虑以下示例。

 $(function() { $("#myDate").datepicker({ changeMonth: true, changeYear: true, dateFormat: "MM yy", showButtonPanel: true, maxDate: "+3m", minDate: "-2y", onClose: function(dateText, inst) { $(this).prop('disabled', true); var m = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); m = parseInt(m) + 1; m = (m < 10 ? "0" + m : m); var y = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); var dt = new Date(y + "-" + m + "-01T00:00:01"); $(this).datepicker( "option", "defaultDate", dt ).datepicker("setDate", dt); //Do something with AJAX $(this).prop('disabled', false); } }).datepicker("setDate", "0"); });
 .ui-datepicker .ui-datepicker-calendar, .ui-datepicker .ui-datepicker-buttonpane .ui-datepicker-current { display: none; }
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> Date: <input type="text" id="myDate" />

In addition to setDate you must also reset the defaultDate .除了setDate您还必须重置defaultDate

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

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