twitter-bootstrap 日期选择器返回未捕获的 typeError: undefined 'currentDay'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17781425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:39:22  来源:igfitidea点击:

Datepicker returning uncaught typeError: undefined 'currentDay'

jquery-uitwitter-bootstrapdatepicker

提问by Bjorn 'Bjeaurn' S

I've been using jQuery UI with Bootstrap and I seem to run into a problem that I haven't had before. I'm not sure what has changed; I've tried setting back different version of jQuery and I didn't update the jQuery UI in the meanwhile. So I'm not exactly sure what broke.

我一直在将 jQuery UI 与 Bootstrap 一起使用,但似乎遇到了以前从未遇到过的问题。我不确定发生了什么变化;我尝试设置回不同版本的 jQuery,但同时我没有更新 jQuery UI。所以我不确定是什么坏了。

The error from the console when I click on any date in the datepicker returns:

当我单击日期选择器中的任何日期时,来自控制台的错误返回:

Uncaught TypeError: Cannot set property 'currentDay' of undefined 

The code is fairly straightforward as one would expect from a datepicker:

正如人们对日期选择器所期望的那样,该代码相当简单:

$(".datepicker").datepicker({
dateFormat: 'dd-mm-yy'
}); 

With the following HTML:

使用以下 HTML:

<input type="text" class="datepicker" />

Is this a bug that should be reported (since no other Google matches turn up) or is it something else I've missed?

这是一个应该报告的错误(因为没有其他谷歌匹配出现)还是我错过的其他东西?

回答by Bjorn 'Bjeaurn' S

I've found the solution. After a long time of debugging I figured out that there was a <div>that had the exact same ID, lying higher than the input field. Therefore the script took the first instance that contained the IDand picked the DIVinstead of the inputfield.

我找到了解决方案。经过长时间的调试,我发现有一个<div>完全相同的ID,位于高于输入字段的位置。因此,脚本采用了包含 的第一个实例ID并选择了DIV而不是input字段。

I removed/renamed the DIVand it worked fine again.

我删除/重命名了DIV它,它再次正常工作。

回答by i--

The above jQuery datepicker error is generally caused by having a duplicate controls with the same ID, no matter whether you use id or another selector like css class or field name to instantiate it.

上面的jQuery datepicker错误一般是因为有相同ID的重复控件造成的,不管你是使用id还是其他选择器,比如css类或字段名来实例化它。

回答by Charles Wyke-Smith

I was having this error also. I found that elsewhere on the form I had some label 'for' attributes that did not match ids on the form inputs - seems like this might confuse datepicker too. Once I fixed those up, everything worked fine and the error went away. Take Andreas' advice (+1 for that!) and validate your forms - Chris Pederick's Web Developer Toolbar gives you an easy way to validate as you go.

我也有这个错误。我发现在表单的其他地方我有一些标签 'for' 属性与表单输入上的 id 不匹配 - 似乎这也可能会混淆日期选择器。一旦我修复了这些,一切正常,错误就消失了。采纳 Andreas 的建议(为此 +1!)并验证您的表单 - Chris Pederick 的 Web 开发人员工具栏为您提供了一种随时进行验证的简单方法。

回答by saswanb

In my case I was triggering the datepicker from a div against a hidden input field and getPos(a) was causing an undefined message on the variable "a". I experimented with moving the hidden input field outside of a div and that did the trick. Note that the input field was findable by jQuery no matter where it was, so that was not the problem - it was with positioning. Hope this helps someone else.

在我的例子中,我从一个隐藏输入字段的 div 中触发了日期选择器,而 getPos(a) 在变量“a”上导致了一条未定义的消息。我尝试将隐藏的输入字段移到 div 之外,结果成功了。请注意,无论输入字段在哪里,jQuery 都可以找到它,所以这不是问题 - 问题在于定位。希望这对其他人有帮助。

回答by gozgunes

put a selector.

放一个选择器。

<script>  $("#formID #duplicatedId").datepicker({language: 'he'});
</script>

FormID: Id of the form that contains input.

FormID:包含输入的表单的 ID。

duplicatedId: input element's Id.

重复 ID:输入元素的 ID。

回答by Sergey

I've ran into this problem too, and i was 100% sure i did not use duplicate id. After some debugging i found out a reason - i was using DataTables mod, which duplicates contents of tfoot, thus creating exact copy of datepicker elements. And i had this datepicker inputs in tfoot row.

我也遇到过这个问题,我 100% 确定我没有使用重复的 ID。经过一些调试后,我发现了一个原因——我使用了 DataTables mod,它复制了 tfoot 的内容,从而创建了 datepicker 元素的精确副本。我在 tfoot 行中有这个日期选择器输入。

Fixed that issue with:

修复了这个问题:

table.find("TFOOT").find("TR:first").remove();

Which is called AFTER table.DataTable(....)

这称为 AFTER table.DataTable(....)

This removed original, zero height row with all contents;

这删除了所有内容的原始零高度行;

回答by Venkatesan

Might be two input box having same class without different id and for that you are trying for date picker. If it is there give two different id it will work.

可能是两个输入框具有相同的类而没有不同的 id,为此您正在尝试使用日期选择器。如果存在,则给出两个不同的 ID,它将起作用。