Bootstrap-Datepicker not selecting date when using "set
Bootstrap-Datepicker not selecting date when using "setDate"
我正在尝试让Bootstarp Datepicker工作,我已经非常接近了。然而,现在我已经找到了如何设置startDate,并且能够让"自动关闭"工作,我无法让它选择在"setDate"中设置的日期
如果我调出日历,然后手动选择另一个将显示为选定的日期,但setDate值不会显示为选定日期。我做了很多搜索,但似乎找不到合适的组合。
顺便说一句:在F12工具中,我确实有一个错误,但它说"加载资源失败:服务器响应404(找不到)",源指向"/fonts/glyphiconshallings-regular"文件。我不认为这与此有关,但我将为此发布一个单独的问题。
这是我的HTML
<div class="col-md-2 text-left">
<div id="StartDate" class="input-group date">
<input class="datepicker form-control" type="text" />
<span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
<div class="col-md-2">
<div id="EndDate" class="input-group date">
<input type="text" class="form-control">
<div class="input-group-addon">
<span class="glyphicon glyphicon-th"></span>
</div>
</div>
</div>
这是我的JS。这个JS会设置日期并使自动关闭工作,但它不会选择用setDate方法设置的日期。
<script type="text/javascript">
$(function () {
var startDate = new Date();
startDate.setDate(startDate.getDate() - 7);
var endDate = new Date();
$("#StartDate").datepicker({
autoclose: true
}).datepicker("update", startDate);
$("#EndDate").datepicker({
autoclose: true
}).datepicker("update", endDate);
});
</script>
如果我切换到这个JS代码,我可以让setDate工作,也可以得到设置为选择的日期。但是,这会导致自动关闭无法工作。
<script type="text/javascript">
$(function () {
var startDate = new Date();
startDate.setDate(startDate.getDate() - 7);
var endDate = new Date();
$("#StartDate").datepicker("setDate", startDate);
$("#StartDate").datepicker("autoclose", true);
$("#StartDate").datepicker("update");
});
</script>
当AutoClose
不工作时,可以使用以下函数强制执行:
$('#StartDate').datepicker().on('changeDate', function(ev){
$('#StartDate').datepicker('hide');
});
$(function () {
var startDate = new Date();
startDate.setDate(startDate.getDate() - 7);
var endDate = new Date();
$("#StartDate").datepicker("setDate", startDate);
$("#StartDate").datepicker("update");
$('#StartDate').datepicker().on('changeDate', function(ev){
$('#StartDate').datepicker('hide');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/datepicker.css">
<div class="col-md-2 text-left">
<div id="StartDate" class="input-group date">
<input class="datepicker form-control" type="text" />
<span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
</div>
相关文章:
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- Ember Data DS.Model's set函数不起作用
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- 将对象设置为Backbone.js模型,而不必调用“;set()"在每一处房产上
- 得到"未捕获的类型错误:未定义的不是函数“;当试图从对象调用get()或set()时
- js未捕获类型错误:Cannot set property 'zIndex'undefined"