Bootstrap Datetimepicker on select event

2019-06-09 17:55发布

问题:

How do I access a js function exampleFunction() when date is selected? I want to pass the date value there.

text_field_tag for datetimepicker (haml):

= text_field_tag 'start_day', class: 'datepick'

Javascript:

$(".datepick").datetimepicker({
  timepicker: false,
  format: 'd/m/Y',
  closeOnDateSelect: true
});

回答1:

If this is the plugin you are using then it would be Like this.

$(".datepick").datetimepicker({
  onChangeDateTime:exampleFunction
});
function exampleFunction(){
  console.log("Date Selected")
}

For full option list Reference: http://xdsoft.net/jqplugins/datetimepicker/

Hope it helps.



回答2:

You can achieve this by using below given code:

$("#datetimepicker6").on("dp.change", function (e) {
     console.log(e.date);
});

I your case, it should be :

$(".datepick").on("dp.change",function(e){ exampleFunction(e.date)  });

Remember this will call the example function on change in value of each datetimepicker, since you used class.

Reference URL: https://eonasdan.github.io/bootstrap-datetimepicker/



回答3:

Try this :

$(".datepick").datetimepicker({
  timepicker: false,
  format: 'd/m/Y',
  closeOnDateSelect: true
}).on('dp.change', function (ev) {
    exampleFunction() ;//your function call
});


回答4:

Simply:

$(".datepick").datetimepicker({
  format: 'd/m/Y',
}).on('select', function (e) {
        console.log('on select event');
    });