Javascript 测试元素是否已经有 jQuery datepicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2579389/
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
Test if element already has jQuery datepicker
提问by macca1
I have a form with many input elements. Some are date fields with a jQuery UI datepicker alraedy attached:
我有一个包含许多输入元素的表单。有些是带有 jQuery UI datepicker alraedy 的日期字段:
$("#someElement").mask("9?9/99/9999").datepicker({showOn: 'button', buttonText:'Click here to show calendar', duration: 'fast', buttonImageOnly: true, buttonImage: /images/calicon.gif' });
Then I have a key command bound to this function:
然后我有一个键命令绑定到这个函数:
function openCalendar() {
var selection = document.activeElement;
// { Need to test here if datepicker has already been initialized
$(selection).datepicker("show");
// }
}
This works great for elements which already have the datepicker on them. However any other fields will throw a javascript error. I'm wondering the best way to test to see if datepicker has already been initialized on that field.
这对于已经有日期选择器的元素非常有用。但是,任何其他字段都会引发 javascript 错误。我想知道测试日期选择器是否已经在该字段上初始化的最佳方法。
回答by macca1
Wow can't believe I missed this one. Line 108 of ui.datepicker.js:
哇不敢相信我错过了这个。ui.datepicker.js 的第 108 行:
/* Class name added to elements to indicate already configured with a date picker. */
markerClassName: 'hasDatepicker',
So I just need to test for hasClass('hasDatepicker'). This seems like the most straightforward way. Furthermore, this statement checks whether the datepicker is currently opened (for anyone who is interested):
所以我只需要测试hasClass('hasDatepicker'). 这似乎是最直接的方式。此外,此语句检查日期选择器当前是否已打开(对于任何感兴趣的人):
if ($("#ui-datepicker-div").is(":visible") && $("#ui-datepicker-div").html() != "") {
// datepicker is open. you need the second condition because it starts off as visible but empty
}
回答by Rafael
1) If the code throws an error when trying to open the datepicker, you can put the code into try..catch
1)如果代码在尝试打开datepicker时抛出错误,可以将代码放入try..catch
2) You can set some data to the input field when initializing the datepicker
2)可以在初始化datepicker的时候给input字段设置一些数据
$("#someElement").data("datepicker-initialized", true);
3) You can use the data stored by datepicker plugin
3) 您可以使用 datepicker 插件存储的数据
if($("#someElement").data("datepicker") != null){
// datepicker initialized
}
or
或者
if($.data($('#someElement').get(0), 'datepicker')){
// datepicker initialized
}
4) datepicker internally uses a function _getInst
4)datepicker内部使用了一个函数_getInst
if($.datepicker._getInst($('#someElement')[0]) != null){}
the method does almost the same as example 3.
该方法与示例 3 几乎相同。
Couldn't find any better solution.
找不到更好的解决办法。
回答by macca1
One basic solution is to set an attribute at time of the datepicker attachment and then test for that:
一种基本的解决方案是在 datepicker 附件时设置一个属性,然后对其进行测试:
$("#someElement").mask("9?9/99/9999")
.datepicker({showOn: 'button', buttonText:'Click here to show calendar', duration: 'fast', buttonImageOnly: true, buttonImage: /images/calicon.gif' })
.attr("data-calendar", "true");
Then you could do:
那么你可以这样做:
if ($(selection).attr("data-calendar") == "true")
$(selection).datepicker("show");
Is there a more direct way?
有没有更直接的方法?
回答by Ahmad
I got another case. My script is copying last table elements including datepicker.
我还有一个案子。我的脚本正在复制最后一个表格元素,包括日期选择器。
The jquery will not working because the copied element has mark that it "hasDatepicker".
jquery 将无法工作,因为复制的元素已标记为“hasDatepicker”。
To activate datepicker in new element, remove that class name and the initiate it, like this.
要在新元素中激活日期选择器,请删除该类名并启动它,如下所示。
$("#yournewelementid").attr("class","your-class-name");
$("#yournewelementid").datepicker();
回答by Zhelyo Hristov
If section is your dom element for datepicker, check if already initialized and if not initialize it first as datepicker (you could add properties) and then show it!
如果 section 是 datepicker 的 dom 元素,请检查是否已初始化,如果未将其初始化为 datepicker(您可以添加属性),然后显示它!
// If already initialized
if ( typeof selection !== 'undefined' && selection.length > 0)
$(selection).datepicker('show');
// otherwise initialize first and show
else
$(selection).datepicker().datepicker('show');
回答by miguelmpn
This was my solution :)
这是我的解决方案:)
if(typeof jQuery.fn.datepicker !== "undefined")

