jQuery 使用 Bootstrap Datepicker 时如何获取选定的日期值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16681875/
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 selected date value while using Bootstrap Datepicker?
提问by Ryan
Using jquery and the Bootstrap Datepicker, how do I get the new date value I selected using the Bootstrap Datepicker? FYI, I'm using Rails 3 and Coffescript.
使用 jquery 和 Bootstrap Datepicker,如何获取我使用 Bootstrap Datepicker 选择的新日期值?仅供参考,我正在使用 Rails 3 和 Coffescript。
I set up the datapicker using:
我使用以下方法设置数据选择器:
<input id="startdate" class="span2 datepicker" type="text" value="" name="startdate" default="2013-05-21" data-date="2013-05-21" data-behavior="datepicker">
<%= submit_tag 'Get Data using date', :id => 'get_data' %>
$(".datepicker").datepicker
endDate: new Date
format: "yyyy-mm-dd"
autoclose: true
minViewMode: 1
todayBtn: "linked"
When a user clicks on the "get data using date" button next to it, I will use jQuery to get the new date value set by the datepicker, prevent the form from submitting and run an ajax request using that date value. All the ajax is running well, except for getting the correct new date value. I tried both of the following and it doesn't give me the new date, it only return the defaults I initially set.
当用户单击旁边的“使用日期获取数据”按钮时,我将使用 jQuery 获取日期选择器设置的新日期值,防止表单提交并使用该日期值运行 ajax 请求。除了获取正确的新日期值之外,所有 ajax 都运行良好。我尝试了以下两种方法,但它没有给我新的日期,它只返回我最初设置的默认值。
sd1 = $('#startdate').attr('value')
console.log sd1
sd2 = $('#startdate').attr('data-date'))
console.log sd2
I am feeling really stupid right now, but I can't find out how to get the new date value set by the bootstrap datepicker.
我现在感觉很愚蠢,但我不知道如何获取引导程序日期选择器设置的新日期值。
回答by Matias
For bootstrap datepicker you can use:
对于引导日期选择器,您可以使用:
$("#inputWithDatePicer").data('datepicker').getFormattedDate('yyyy-mm-dd');
回答by Sushanth --
You can try this
你可以试试这个
$('#startdate').val()
or
或者
$('#startdate').data('date')
回答by Luca Rasconi
Bootstrap datepicker (the first result from bootstrap datepickcer search) has a method to get the selected date.
https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#getdate
Bootstrap datepicker(bootstrap datepickcer 搜索的第一个结果)有一个方法来获取所选日期。
https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#getdate
getDate: Returns a localized date object representing the internal date object of the first datepicker in the selection. For multidate pickers, returns the latest date selected.
getDate:返回一个本地化的日期对象,表示选择中第一个日期选择器的内部日期对象。对于多日期选择器,返回选择的最新日期。
$('.datepicker').datepicker("getDate")
or
或者
$('.datepicker').datepicker("getDate").valueOf()
回答by Lodato L
Generally speaking,
通常来说,一般来说,
$('#startdate').data('date')
is best because
最好是因为
$('#startdate').val()
not work if you have a datepicker "inline"
如果您有“内联”日期选择器,则不起作用
回答by Shady
this works with me for inline datepicker
(and the other)
这适用于我的内联datepicker
(和其他)
$('#startdate').data('datepicker').date
回答by Pedro Martins
If you wan't the date in full text string format you can do it like this:
如果您不需要全文字符串格式的日期,您可以这样做:
$('#your-datepicker').data().datepicker.viewDate
回答by Bern
If you already have a number of dates already highlightedand want to determine which date was last clickedthen you'll need the following:
如果您已经突出显示了多个日期,并且想要确定上次单击哪个日期,那么您需要以下内容:
$('#startdate').data('datepicker').viewDate
viewDate
returns a JavaScript date object so you'll need to handle it accordingly.
viewDate
返回一个 JavaScript 日期对象,因此您需要相应地处理它。
回答by user3475960
Try this using HTML like here:
使用 HTML 尝试此操作,如下所示:
var myDate = window.document.getElementById("startdate").value;
回答by SMAG
回答by Developer
There are many solutions here but probably the best one that works. Check the version of the script you want to use.
这里有很多解决方案,但可能是最好的解决方案。检查您要使用的脚本版本。
Well at least I can give you my 100% working solution for
好吧,至少我可以给你我 100% 的工作解决方案
version : 4.17.45
bootstrap-datetimejs https://github.com/Eonasdan/bootstrap-datetimepickerCopyright (c) 2015 Jonathan Peterson
版本:4.17.45
bootstrap-datetimejs https://github.com/Eonasdan/bootstrap-datetimepicker版权所有 (c) 2015 Jonathan Peterson
JavaScript
JavaScript
var startdate = $('#startdate').val();
The output looks like: 12.09.2018 03:05
输出看起来像:12.09.2018 03:05