Bootstrap日期时间选择器第二次不更新

huangapple go评论75阅读模式
英文:

Bootstrap datetime picker not updating second time

问题

我想要在叫做pickup_time_picker的日期选择器中设置一个起始日期选择器,名为dropoff_date_picker。当我首次从pickup_time_picker中选择日期时,dropoff_date_picker的起始日期会更新并正常工作,但如果用户从pickup_time_picker中更改日期,那么dropoff_time_picker将不会更新,并且第二次不起作用。以下是我的代码片段。我不知道我在哪里出错了。我已经尝试了'change'、'dp.change'、'change.dp'和'changeDate',但什么都不起作用。

$("#pickup_time_picker").datetimepicker({
  format: "dd MM yyyy - hh:ii",
  autoclose: true,
  todayBtn: true,
  startDate: today,
  minuteStep: 10,
  pickerPosition: "bottom-left"
}).on('change.dp', function(e) {

  var fromDate = $('#pickup_time_feild').val().split(" ");
  var fromTime = $('#pickup_time_feild').val().split(" - ");
  var fromMinutes = fromTime[1].split(":");
  var dat = new Date(fromDate[0] + ' ' + fromDate[1] + ' ' + fromDate[2] + '');
  var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], fromMinutes[1]);
  enddate = new Date(present.getFullYear(), present.getMonth() - 1, present.getDate(), present.getHours(),
    present.getMinutes(), 0, 0);

  alert(present);

  $('#dropoff_time_picker').datetimepicker({
    format: "dd MM yyyy - hh:ii",
    autoclose: true,
    todayBtn: true,
    startDate: present,
    minuteStep: 10,
    pickerPosition: "bottom-left"
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="date_wrap">
  <input type="date" id="pickup_time_picker" />
  <input type="date" id="dropoff_time_picker" />
</div>

请注意,这段代码可能存在一些问题,例如变量名和选择器名称可能会导致错误。您需要确保选择器名称和 HTML 元素的 ID 匹配,以确保代码能够正常运行。

英文:

I want to set a start date picker which is called dropoff_date_picker from another which is pickup_time_picker. when I select a date from pickup_time_picker first-time dropoff_date_picker start date updated and works fine but if the user changes the date from pickup_time_picker than dropoff_time_picker will not be updated and it will not work second time below is my code snippet. I don't know where I am doing mistake. I have already used 'change', dp.change','change.dp' and 'changeDate' and nothing is working.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#pickup_time_picker&quot;).datetimepicker({
  format: &quot;dd MM yyyy - hh:ii&quot;,
  autoclose: true,
  todayBtn: true,
  startDate: today,
  minuteStep: 10,
  pickerPosition: &quot;bottom-left&quot;
}).on(&#39;change.dp&#39;, function(e) {

  var fromDate = $(&#39;#pickup_time_feild&#39;).val().split(&quot; &quot;);
  var fromTime = $(&#39;#pickup_time_feild&#39;).val().split(&quot; - &quot;);
  var fromMinutes = fromTime[1].split(&quot;:&quot;);
  var dat = new Date(fromDate[0] + &#39; &#39; + fromDate[1] + &#39; &#39; + fromDate[2] + &#39;&#39;);
  var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], fromMinutes[1]);
  enddate = new Date(present.getFullYear(), present.getMonth() - 1, present.getDate(), present.getHours(),
    present.getMinutes(), 0, 0);

  alert(present);

  $(&#39;#dropoff_time_picker&#39;).datetimepicker({
    format: &quot;dd MM yyyy - hh:ii&quot;,
    autoclose: true,
    todayBtn: true,
    startDate: present,
    minuteStep: 10,
    pickerPosition: &quot;bottom-left&quot;
  });

});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;date_wrap&quot;&gt;
  &lt;input type=&quot;date&quot; id=&quot;pickup_time_picker&quot; /&gt;
  &lt;input type=&quot;date&quot; id=&quot;dropoff_time_picker&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我刚刚在最后添加了以下行,并且它有效。

$(&#39;#dropoff_time_picker&#39;).datetimepicker(&#39;setStartDate&#39;, present);

以下是完整的代码。

$(&quot;#pickup_time_picker&quot;).datetimepicker({
   format: &quot;dd MM yyyy - hh:ii&quot;,
   autoclose: true,
   todayBtn: true,
   startDate: today,
   minuteStep: 10,
   pickerPosition: &quot;bottom-left&quot;
}).on(&#39;change.dp&#39;, function(e) {

   var fromDate = $(&#39;#pickup_time_feild&#39;).val().split(&quot; &quot;);
   var fromTime = $(&#39;#pickup_time_feild&#39;).val().split(&quot; - &quot;);
   var fromMinutes = fromTime[1].split(&quot;:&quot;);
   var dat = new Date(fromDate[0] + &#39; &#39; + fromDate[1] + &#39; &#39; + fromDate[2] + &#39;&#39;);
   var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], 
   fromMinutes[1]);
   enddate = new Date(present.getFullYear(), present.getMonth() - 1, 
   present.getDate(), present.getHours(),
   present.getMinutes(), 0, 0);
   $(&#39;#dropoff_time_picker&#39;).datetimepicker({
   format: &quot;dd MM yyyy - hh:ii&quot;,
   autoclose: true,
   todayBtn: true,
   startDate: present,
   minuteStep: 10,
   pickerPosition: &quot;bottom-left&quot;
  });
 $(&#39;#dropoff_time_picker&#39;).datetimepicker(&#39;setStartDate&#39;, present);
});

如果您需要进一步的帮助,请告诉我。

英文:

I just added following line in the end and it works.

$(&#39;#dropoff_time_picker&#39;).datetimepicker(&#39;setStartDate&#39;,present);

Following is the complete code.

$(&quot;#pickup_time_picker&quot;).datetimepicker({
format: &quot;dd MM yyyy - hh:ii&quot;,
autoclose: true,
todayBtn: true,
startDate: today,
minuteStep: 10,
pickerPosition: &quot;bottom-left&quot;
}).on(&#39;change.dp&#39;, function(e) {
var fromDate = $(&#39;#pickup_time_feild&#39;).val().split(&quot; &quot;);
var fromTime = $(&#39;#pickup_time_feild&#39;).val().split(&quot; - &quot;);
var fromMinutes = fromTime[1].split(&quot;:&quot;);
var dat = new Date(fromDate[0] + &#39; &#39; + fromDate[1] + &#39; &#39; + fromDate[2] + &#39;&#39;);
var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], 
fromMinutes[1]);
enddate = new Date(present.getFullYear(), present.getMonth() - 1, 
present.getDate(), present.getHours(),
present.getMinutes(), 0, 0);
$(&#39;#dropoff_time_picker&#39;).datetimepicker({
format: &quot;dd MM yyyy - hh:ii&quot;,
autoclose: true,
todayBtn: true,
startDate: present,
minuteStep: 10,
pickerPosition: &quot;bottom-left&quot;
});
$(&#39;#dropoff_time_picker&#39;).datetimepicker(&#39;setStartDate&#39;,present);
});

huangapple
  • 本文由 发表于 2020年1月3日 13:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573813.html
  • bootstrap-4
  • datetimepicker
  • html
  • jquery
  • twitter-bootstrap-3
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定