英文:
Bootstrap datetime picker not updating second time
问题
我想要在叫做pickup_time_picker的日期选择器中设置一个起始日期选择器,名为dropoff_date_picker。当我首次从pickup_time_picker中选择日期时,dropoff_date_picker的起始日期会更新并正常工作,但如果用户从pickup_time_picker中更改日期,那么dropoff_time_picker将不会更新,并且第二次不起作用。以下是我的代码片段。我不知道我在哪里出错了。我已经尝试了'change'、'dp.change'、'change.dp'和'changeDate',但什么都不起作用。
$("#pickup_time_picker").datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: today,
minuteStep: 10,
pickerPosition: "bottom-left"
}).on('change.dp', function(e) {
var fromDate = $('#pickup_time_feild').val().split(" ");
var fromTime = $('#pickup_time_feild').val().split(" - ");
var fromMinutes = fromTime[1].split(":");
var dat = new Date(fromDate[0] + ' ' + fromDate[1] + ' ' + fromDate[2] + '');
var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], fromMinutes[1]);
enddate = new Date(present.getFullYear(), present.getMonth() - 1, present.getDate(), present.getHours(),
present.getMinutes(), 0, 0);
alert(present);
$('#dropoff_time_picker').datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: present,
minuteStep: 10,
pickerPosition: "bottom-left"
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="date_wrap">
<input type="date" id="pickup_time_picker" />
<input type="date" id="dropoff_time_picker" />
</div>
请注意,这段代码可能存在一些问题,例如变量名和选择器名称可能会导致错误。您需要确保选择器名称和 HTML 元素的 ID 匹配,以确保代码能够正常运行。
英文:
I want to set a start date picker which is called dropoff_date_picker from another which is pickup_time_picker. when I select a date from pickup_time_picker first-time dropoff_date_picker start date updated and works fine but if the user changes the date from pickup_time_picker than dropoff_time_picker will not be updated and it will not work second time below is my code snippet. I don't know where I am doing mistake. I have already used 'change', dp.change','change.dp' and 'changeDate' and nothing is working.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#pickup_time_picker").datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: today,
minuteStep: 10,
pickerPosition: "bottom-left"
}).on('change.dp', function(e) {
var fromDate = $('#pickup_time_feild').val().split(" ");
var fromTime = $('#pickup_time_feild').val().split(" - ");
var fromMinutes = fromTime[1].split(":");
var dat = new Date(fromDate[0] + ' ' + fromDate[1] + ' ' + fromDate[2] + '');
var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0], fromMinutes[1]);
enddate = new Date(present.getFullYear(), present.getMonth() - 1, present.getDate(), present.getHours(),
present.getMinutes(), 0, 0);
alert(present);
$('#dropoff_time_picker').datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: present,
minuteStep: 10,
pickerPosition: "bottom-left"
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="date_wrap">
<input type="date" id="pickup_time_picker" />
<input type="date" id="dropoff_time_picker" />
</div>
<!-- end snippet -->
答案1
得分: 1
我刚刚在最后添加了以下行,并且它有效。
$('#dropoff_time_picker').datetimepicker('setStartDate', present);
以下是完整的代码。
$("#pickup_time_picker").datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: today,
minuteStep: 10,
pickerPosition: "bottom-left"
}).on('change.dp', function(e) {
var fromDate = $('#pickup_time_feild').val().split(" ");
var fromTime = $('#pickup_time_feild').val().split(" - ");
var fromMinutes = fromTime[1].split(":");
var dat = new Date(fromDate[0] + ' ' + fromDate[1] + ' ' + fromDate[2] + '');
var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0],
fromMinutes[1]);
enddate = new Date(present.getFullYear(), present.getMonth() - 1,
present.getDate(), present.getHours(),
present.getMinutes(), 0, 0);
$('#dropoff_time_picker').datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: present,
minuteStep: 10,
pickerPosition: "bottom-left"
});
$('#dropoff_time_picker').datetimepicker('setStartDate', present);
});
如果您需要进一步的帮助,请告诉我。
英文:
I just added following line in the end and it works.
$('#dropoff_time_picker').datetimepicker('setStartDate',present);
Following is the complete code.
$("#pickup_time_picker").datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: today,
minuteStep: 10,
pickerPosition: "bottom-left"
}).on('change.dp', function(e) {
var fromDate = $('#pickup_time_feild').val().split(" ");
var fromTime = $('#pickup_time_feild').val().split(" - ");
var fromMinutes = fromTime[1].split(":");
var dat = new Date(fromDate[0] + ' ' + fromDate[1] + ' ' + fromDate[2] + '');
var present = new Date(fromDate[2], dat.getMonth(), fromDate[0], fromMinutes[0],
fromMinutes[1]);
enddate = new Date(present.getFullYear(), present.getMonth() - 1,
present.getDate(), present.getHours(),
present.getMinutes(), 0, 0);
$('#dropoff_time_picker').datetimepicker({
format: "dd MM yyyy - hh:ii",
autoclose: true,
todayBtn: true,
startDate: present,
minuteStep: 10,
pickerPosition: "bottom-left"
});
$('#dropoff_time_picker').datetimepicker('setStartDate',present);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论