最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS DateTimePicker onChange event not working - Stack Overflow

programmeradmin9浏览0评论

JS datetimepicker onChange event not working at all.

I've tried OnSelect, OnClose, OnChange, OnHide, OnChangeMonth to trigger datetimepicker event, but none of them works.

<input type="text" class="form-control datetimepicker-input" id="Datetimepicker" data-toggle="datetimepicker" data-target="#Datetimepicker" autoplete="off" style="width: 200px;" />

$("#Datetimepicker").datetimepicker({
    format: 'MMM,YYYY',
    defaultDate: today,
    onSelect: function () {
        alert("It works!");
    }
})

$("#Datetimepicker").datetimepicker("change",function(){
    alert("It works!")});


$("#Datetimepicker").datetimepicker("changeDate",function(){
    alert("It works!")});

I've tried many ways, but still none of them working. Do I need to provide some other js library in order to make it work? Appreciate your help.

JS datetimepicker onChange event not working at all.

I've tried OnSelect, OnClose, OnChange, OnHide, OnChangeMonth to trigger datetimepicker event, but none of them works.

<input type="text" class="form-control datetimepicker-input" id="Datetimepicker" data-toggle="datetimepicker" data-target="#Datetimepicker" autoplete="off" style="width: 200px;" />

$("#Datetimepicker").datetimepicker({
    format: 'MMM,YYYY',
    defaultDate: today,
    onSelect: function () {
        alert("It works!");
    }
})

$("#Datetimepicker").datetimepicker("change",function(){
    alert("It works!")});


$("#Datetimepicker").datetimepicker("changeDate",function(){
    alert("It works!")});

I've tried many ways, but still none of them working. Do I need to provide some other js library in order to make it work? Appreciate your help.

Share Improve this question edited Oct 29, 2019 at 4:29 Jet asked Oct 29, 2019 at 3:42 JetJet 451 gold badge3 silver badges9 bronze badges 5
  • Are you using this datepicker? – Manuel Abascal Commented Oct 29, 2019 at 3:49
  • Hi, I'm actually using bootstrap-4 datetimepicker – Jet Commented Oct 29, 2019 at 3:54
  • Can you share the link please? – Manuel Abascal Commented Oct 29, 2019 at 3:56
  • Sure! tempusdominus.github.io/bootstrap-4/Usage – Jet Commented Oct 29, 2019 at 3:57
  • I'll work on it. Please add your HTML code as well to your question. – Manuel Abascal Commented Oct 29, 2019 at 4:00
Add a ment  | 

4 Answers 4

Reset to default 12

As mentioned by @Bluety you have to use the events provided by the library.

In your case you can use change.datetimepicker event. The event handler function receives old date and new date which can be used for any parisons you might need.

$("#Datetimepicker").on("change.datetimepicker", ({date, oldDate}) => {              
                 //Use date and oldDate here
          })

Also note that, in your case the input element is same as datepicker target, This can lead to duplicate events triggered

发布评论

评论列表(0)

  1. 暂无评论