[英]How to use Date Range Picker to pick a date and put it in a input
I am trying to use this addon http://www.daterangepicker.com/ but i can't figure out how retreive the value in an input 我正在尝试使用此插件http://www.daterangepicker.com/,但我不知道如何在输入中获取值
In this example it show us how to do it 在这个例子中,它告诉我们如何做
<input type="text" name="birthdate" value="10/24/1984" />
<script type="text/javascript">
$(function() {
$('input[name="birthdate"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true
},
function(start, end, label) {
var years = moment().diff(start, 'years');
alert("You are " + years + " years old.");
});
});
</script>
The value selected by user will go to the input birthdate
用户选择的值将转到输入的birthdate
But in this example (the one that i want to use ) 但是在此示例中(我要使用的示例)
<div id="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
<span></span> <b class="caret"></b>
</div>
<script type="text/javascript">
$(function() {
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
cb(moment().subtract(29, 'days'), moment());
$('#reportrange').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
});
</script>
There is no input 没有输入
I don't know how to retreive the input ! 我不知道如何检索输入!
If I understand correctly, you want to show the selected dates in a label, and save the selection in variable so you can access it. 如果我理解正确,则希望在标签中显示所选日期,并将所选内容保存在变量中,以便可以访问它。 Here is how I did it: 这是我的做法:
The html: 的HTML:
<p>
<label for="dateRange">Choose timeframe</label>
</p>
<div id="dateRange" class="btn default">
<i class="fa fa-calendar"></i>
<span> </span>
<b class="fa fa-angle-down"></b>
</div>
And here is the js: 这是js:
var startDate = moment().subtract('month', 1).startOf('month'),
endDate = moment().subtract('month', 1).endOf('month');
$('#dateRange').daterangepicker({
opens: (App.isRTL() ? 'left' : 'right'),
startDate: startDate,
endDate: endDate,
dateLimit: {
years: 1
},
showDropdowns: true,
showWeekNumbers: true,
timePicker: false,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
'Last 7 Days': [moment().subtract('days', 6), moment()],
'Last 30 Days': [moment().subtract('days', 29), moment()],
'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
},
autoApply: true,
format: 'MM/DD/YYYY',
separator: ' to ',
locale: {
applyLabel: 'Apply',
fromLabel: 'From',
toLabel: 'To',
customRangeLabel: 'Custom Range',
daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
firstDay: 1
}
},
function (start, end) {
// updating the span with current dates
$('#dateRange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
// Saving the new dates in your startDate and endDate variables.
startDate = start;
endDate = end;
}
);
//Set the initial state of the picker label
$('#dateRange span').html(startDate.format('MMMM D, YYYY') + ' - ' + endDate.format('MMMM D, YYYY'));
Where startDate and endDate is moment() objects of my choosing. 其中startDate和endDate是我选择的moment()对象。 The picker will look like this: 选择器将如下所示:
To retrieve the value: 要获取值:
For example to unix timestamp: 例如Unix时间戳:
var startUnix = startDate.unix();
var endUnix = endDate.unix();
Or using any format function avalible in moment js: moment js format options 或使用时刻js中可用的任何格式函数: 时刻js格式选项
I suggest you read through the daterangepicker options as well so you know what every option does. 我建议您也仔细阅读daterangepicker选项 ,以了解每个选项的作用。
And then to send it in a request or anything: 然后以请求或其他方式发送它:
var params = {};
params.start = startUnix;
params.end = endUnix;
$.get('url', params).done(function (data) {
// callback
});
Could you try adding an input field 您可以尝试添加输入字段吗
<div id="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>
<!-- here or where is necessary -->
<input type="text" name="birthdate" value="10/24/1984" />
<span></span> <b class="caret"></b>
</div>
<script type="text/javascript">
$(function() {
function cb(start, end) {
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
}
cb(moment().subtract(29, 'days'), moment());
$('#reportrange').daterangepicker({
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
}
}, cb);
});
</script>
The date value selected will get populated in the empty <span></span>
. 所选的日期值将填充在空白的<span></span>
。 For example <span>January 29, 2016 - February 4, 2016</span>
例如<span>January 29, 2016 - February 4, 2016</span>
<input id="hdnReportRange" type="hidden"/>
... ...
$("#reportrange").on("apply.daterangepicker", function(ev, picker) {
$("#hdnReportRange").val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.