javascript 如何从日历 eonasdan 中获取当前选定日期的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32223296/
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
How to get the value of current selected date from calendar eonasdan
提问by Tausif Anwar
Hi guys i am using http://eonasdan.github.io/bootstrap-datetimepicker/for calendar, now i want to pick the selected date by the user, I am stuck with this calendar please help me to get the current selected date by the user i have used js to get the value. On each date there is a class named "day" now i wanted to get the value of the attribute "data-day" which have the value of the date as follows
大家好,我正在使用http://eonasdan.github.io/bootstrap-datetimepicker/作为日历,现在我想选择用户选择的日期,我被这个日历卡住了,请帮我获取当前选择的日期我使用 js 获取值的用户。在每个日期都有一个名为“day”的类,现在我想获取属性“data-day”的值,该值具有如下日期值
$(".day").click(function(){
var clicked = $(this).attr('id');
alert(clicked);
});
It returns the attribute's value only one time it's not recognizing the second click. Please help me with js or this eonasdan plugin.
它仅在无法识别第二次点击时返回属性值。请帮助我使用 js 或这个 eonasdan 插件。
采纳答案by J Santosh
HTML
HTML
<br/>
<!-- padding for jsfiddle -->
<div class="row">
<div class="col-md-4 col-xs-6">
<input type="text" class="form-control" id="datetimepicker2" />
<br>
<input type="button" class="btn btn-success" id="getDate" value="Get Date" />
</div>
<span id="SelectedDate"></span>
</div>
JS
JS
$('#datetimepicker2').datetimepicker();
$('#getDate').click(function () {
console.log($('#datetimepicker2').data('date'))
$('#SelectedDate').text($('#datetimepicker2').data('date'))
})
回答by Illia Olenchenko
I have found methods for calendar described at linked page. So for example to see what have user entered you have to:
我找到了链接页面中描述的日历方法。因此,例如要查看用户输入的内容,您必须:
$('#datetimepicker').data("DateTimePicker").date()
input here your id and as result receive object which has '_d' field with value.
在此处输入您的 id,结果接收具有 '_d' 字段值的对象。
data('DateTimePicker')
also has a lot of taste functions to configure plugin.
也有很多品味功能来配置插件。
回答by Hudhaifa Yoosuf
i think i might have the solution for you.
我想我可以为您提供解决方案。
$('#datetimepicker1').data('date')
Please have a go.
请试一试。