jQuery Bootstrap Datetimepicker 在类上触发“dp.change”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27398028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:24:11  来源:igfitidea点击:

Bootstrap Datetimepicker Trigger "dp.change" on Class

jquerytwitter-bootstrapdatepicker

提问by user2002495

I'm using thispackage for my datepicker. I'm initiating the plugin like this (CS):

我正在为我的日期选择器使用这个包。我正在启动这样的插件(CS):

$('.datepicker').datetimepicker(
    pickTime: false
    language: 'en'
).on 'dp.change', (e) ->
    console.log 'this'

The datepicker works fine, but the dp.change event is not triggered, anyone can explain why?

日期选择器工作正常,但未触发 dp.change 事件,谁能解释为什么?

[DIRTY SOLUTION]

[肮脏的解决方案]

For now I'm using something like this, very ugly solution though:

现在我正在使用这样的东西,但非常丑陋的解决方案:

$('.bootstrap-datetimepicker-widget').on "click", "td.day", ->
    date = $('.datepicker input[type=text]').val()
    $('.datepicker p').find('span').text date

回答by Adam Touhou

The documentations https://eonasdan.github.io/bootstrap-datetimepicker/Gives you an example on how to do this. (Just tested it and if you copy+paste the code, it will work fine.)

文档 https://eonasdan.github.io/bootstrap-datetimepicker/为您提供了如何执行此操作的示例。(刚刚测试过,如果你复制+粘贴代码,它会正常工作。)

        $("#datetimepicker6").on("dp.change", function (e) {
//do your action here
        });

The ID of the jquery selector, you put the element you activated the datepicker for (i.e. initiated it like:

jquery 选择器的 ID,您放置了为其激活日期选择器的元素(即像这样启动它:

$('#datetimepicker6').datetimepicker();

) Let me know if it didn't work out for you :-)

) 如果它不适合您,请告诉我:-)

回答by Developer

If you don't know the DOMIDof the element, you may listen to events on the document:

如果您不知道DOMID元素的 ,您可以监听 上的事件document

$(document).on('dp.change', function (e) {
  // here `e` is an event itself.
  // So, `e.target` should be a DOM element of the input field.
  if ($(e.target).data('object') === 'calendar1') {
      console.log('calendar1 just changed');
  } else {
      console.log('nope, we are waiting for calendar1 changes only');
  }
});

I hope this helps.

我希望这有帮助。

回答by Mohamed Hana

This is an old thread but I just faced the same problem and I did solve it by adding change event to the input.

这是一个旧线程,但我刚刚遇到了同样的问题,我确实通过向输入添加更改事件来解决它。

$("body").on("change dp.change", "#your_selector", function(event){
     var date = $("#your_selector").data("datetimepicker").getDate();
     console.log(date);
});

回答by Deepak Panangatt

This is a very old thread to comment but I think the answers are a bit confusing. to get the selected date you can use

这是一个非常古老的评论线程,但我认为答案有点令人困惑。获取您可以使用的选定日期

$('.datepicker').on('dp.change', function(e){
//To get moment object
console.log(e.date);
//To format date into DD/MM/YYYY
console.log(e.date.format('DD/MM/YYYY'));
});

回答by writeToBhuwan

Seems like you forgot to add moment.js(before the datetimepicker.js) in your page. Add the script and it should work just fine.

好像您忘记在页面中添加moment.js(在 datetimepicker.js 之前)。添加脚本,它应该可以正常工作。