如何在Microsoft Edge中使用Bootstrap Datepicker

时间:2016-06-27 15:43:01

标签: javascript jquery css twitter-bootstrap datepicker

我在使用Edge专门针对Bootstrap Datepicker时遇到了问题。 Chrome,Firefox和Internet Explorer版本11按预期工作,但Edge正在做一些非常奇怪且不受欢迎的事情。

我尝试使用jQuery UI datepicker但我没有取得任何成功,所以我回滚到Bootstrap。有没有办法让Bootstrap datepicker与Edge一起使用?或者,是否有另一种日期选择技术可行?我真的希望不需要任何hacky。

在我的Razor View中(在MVC网站项目中)我有:

@Html.EditorFor(model => model.DateCreditEarned, 
                new { htmlAttributes = new { @class = "form-control" } })

在视图的底部我有:

@section styles {        
<link href="~/Content/bootstrap-datepicker3.css" rel="stylesheet" />
}

@section scripts {
<script src="~/Scripts/bootstrap-datepicker.js"></script>
    <script>
        $(document).ready(function () {
            $("#DateCreditEarned").datepicker();
        });
    </script>
}

这是Datepicker在Chrome中按预期工作的屏幕截图。 Firefox和IE版本11也按预期工作。

Bootstrap Datepicker using Chrome

这是Edge中的Datepicker的屏幕截图,它没有按预期或期望工作。它不仅视觉上很丑陋,点击任何日期值都不会触发任何操作。

我注意到,再试一次,Bootstrap日期选择器出现了几分之一秒,然后被你在截图中看到的重叠。看起来Edge正在自己的日期选择器中滑落。

Bootstrap Datepicker using Edge

1 个答案:

答案 0 :(得分:4)

问题是Edge有date类型输入的默认样式和选择器。该默认样式优先于通过插件完成的其他样式。比较这个没有“日期”类型:

$('#DateCreditEarned').datepicker();
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/bootstrap-datepicker3.min.css">
<script src="https://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>

<input id="DateCreditEarned" class="form-control" />

VS。这个使用“日期”类型:

$('#DateCreditEarned').datepicker();
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/bootstrap-datepicker3.min.css">
<script src="https://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>

<input id="DateCreditEarned" type="date" class="form-control" />

要解决此问题,请尝试将输入类型设置为"text"。这样,所有控件和样式都由插件而不是Edge处理。

您可以使用以下方法在Razor中完成此操作:

@Html.EditorFor(model => model.DateCreditEarned, new { 
    htmlAttributes = new { @type = "text", @class = "form-control" } 
})