twitter-bootstrap 如何从 bootstrap-datepicker 获取更改的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20413777/
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-10-21 19:24:57  来源:igfitidea点击:

How to get the changed date from bootstrap-datepicker

javascriptjquerytwitter-bootstrapbootstrap-datepicker

提问by hasan.alkhatib

I am using Eternicode's fork of the bootstrap-datepicker libraryand until now all is going well for generating it, but I want to get the date in order to use it in my logic after changing the date.

我正在使用Eternicode 的 bootstrap-datepicker 库的分支,到目前为止,生成它的一切进展顺利,但我想获取日期,以便在更改日期后在我的逻辑中使用它。

$('#calendar').on('changeDate', function(event, date) {
  // how to pop alert message to print the date I've chosen ?
});

How can I do this?

我怎样才能做到这一点?

回答by Mark Amery

As described in the docs, the Eternicode version of bootstrap-datepicker adds three attributes to the changeDateevent that can be used for accessing the newly selected date. You'll be interested in either the .dateproperty (which points to the actual date objectrepresenting the selected date) or the .formatmethod, which you can use to get the date as a string.

文档中所述,bootstrap-datepicker 的 Eternicode 版本向changeDate事件添加了三个属性,可用于访问新选择的日期。您将对.date属性(指向代表所选日期的实际日期对象)或.format方法感兴趣,您可以使用该方法将日期作为字符串获取。

Assuming the simplest case (where you have a single picker and you want to get the date as a string in the same format that you initialised the datepicker with), you'll just want to call .format()with no arguments:

假设最简单的情况(您有一个选择器,并且您希望以与初始化日期选择器相同的格式将日期作为字符串获取),您只需要不带.format()参数调用:

$('#calendar').on('changeDate', function(event) {
    alert(event.format());
});

Here's a JSFiddle showing this in action: http://jsfiddle.net/bhm7p/3/

这是一个 JSFiddle,显示了这一点:http: //jsfiddle.net/bhm7p/3/

回答by krishna kachhela

Put this code in the jquery function body:

将此代码放在 jquery 函数体中:

$(document).ready(function () {
  $('#datepicker').on('dp.change', function(){
    var t=Date.parse($("#dob1").val());    
    if (t > Date.now()) {
      alert('Selected date shold not be a future date');
      $("#dob1").val('');
    }
  });
});

回答by mxmxwo

There are two things you could try.

您可以尝试两件事。

Try implementing this line:

尝试实现这一行:

$('#startdate').val()

Or this one:

或者这个:

$('#startdate').data('date')

Drew T. suggests using .val() - also very efficient.

Drew T. 建议使用 .val() - 也非常有效。

$('#calendar').on('changeDate', function(event, date) {
    var date = $('#calendar').val();
    alert(date);
});

Hope this helps!

希望这可以帮助!

回答by faiz

actually the fundamental is like this:
you will get access to the date by the .onevent that parsed to the eparameter. from eyou can't access to .getDate

实际上基本原理是这样的:
您将通过.on解析为e参数的事件访问日期。从e你不能访问.getDate

$('#calendar').on('changeDate', function(e) {
    alert(e.getDate()+'/'+e.getMonth()+e.getFullYear());
});

if you can't understand how this happened, you can put the response in console.log(e)rather than alert(e.getDate)so you can get a clear view on what to access.

如果您无法理解这是如何发生的,您可以输入响应console.log(e)而不是alert(e.getDate)这样您就可以清楚地了解要访问的内容。