Javascript 如何在不打开日历的情况下动态更改日期
我正在使用Javascript 如何在不打开日历的情况下动态更改日期,javascript,jquery,datepicker,bootstrap-datepicker,Javascript,Jquery,Datepicker,Bootstrap Datepicker,我正在使用bootstrap datepicker并创建了一个函数,将datepicker日期重置为今天,一切正常,只是当我调用我的函数时,它会自动打开日历框,日历框保持在页面上,即使失去焦点也不会消失。如何避免此问题,或者在日期重置时不自动打开日历(我尝试了堆栈中的一些解决方案,但没有任何效果…) 日期选择器配置 $('#sandbox-container .input-group.date').datepicker({ language: "he", rtl: true, aut
bootstrap datepicker
并创建了一个函数,将datepicker日期重置为今天,一切正常,只是当我调用我的函数时,它会自动打开日历框,日历框保持在页面上,即使失去焦点也不会消失。如何避免此问题,或者在日期重置时不自动打开日历(我尝试了堆栈中的一些解决方案,但没有任何效果…)
日期选择器配置
$('#sandbox-container .input-group.date').datepicker({
language: "he",
rtl: true,
autoclose: true,
todayHighlight: true,
format: "dd/mm/yyyy"
});
重置日期的功能
function resetStartDatePicker() {
//option 1 - didn't work
$("#sandbox-container").focusout(function () {
$("#sandbox-container").hide;
});
//option 2 - didn't work
$("#sandbox-container ~ .ui-datepicker").hide();
//option 3 - didn't work
$("#sandbox-container").datepicker().on('changeDate', function () {
$(this).blur();
});
//option 4 - didn't work
$.fn.datepicker.defaults.autoclose = true;
var today = getToday();
$("#sandbox-container").find("input").val(today.a);
$("#sandbox-container").datepicker("update", today.b);
}
HTML
<div id="sandbox-container" >
<div id="positionSorter" style="margin-top:10%">
<a id="anotherDatelink" title="reset" class="pull-left" href="#" onclick="resetStartDatePicker()">reset</a>
</div>
<div class="input-group date" data-provide="datepicker" style="float:right">
<input type="text" class="form-control @semanticClass" name="@Model.Name" id="@Model.Id" title="@Model.Name" value="@val">
<div class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</div>
</div>
</div>
您正在将日期选择器附加到div元素,实际上这就是问题所在 为文本框指定id,如
<input id="txtDate" type="text" class="form-control @semanticClass" name="@Model.Name" id="@Model.Id" title="@Model.Name" value="@val">
或者,如果您想使用您提供的like,只需更改like即可
$('#sandbox-container input').datepicker({
language: "he",
rtl: true,
autoclose: true,
todayHighlight: true,
format: "dd/mm/yyyy"
});
上面的代码从sandbox容器div中查找输入元素。下面是一个示例,说明如何设置值,然后将其隐藏-
$('#anotherDatelink').on('click', function(e) {
$("#sandbox-container").find("input").val('01/01/2010');
$("#sandbox-container .input-group").datepicker('hide');
});
在检查了datepicker之后,解决我问题的方法是:
$(".datepicker-days").hide();
谢谢你的帮助,我喜欢你所有的答案如果可以,请提供任何代码示例或小提琴。以便用户能够了解问题所在。还有,您也在使用哪个日期选择器。@BibyAugustine我在使用引导日期选择器。应该将其隐藏:$(“#沙箱容器输入”)。日期选择器('hide');您是否使用日期选择器的文本框?它可能重复,并显示从第一秒开始的日历。。。并没有隐藏它:/
$(".datepicker-days").hide();