javascript 如何获取 jquery datepicker 的所有选项以使用相同的选项实例化新的 datepicker?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17727845/
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
How to get all options of jquery datepicker to instanciate new datepicker with same options?
提问by Franck S
How to get all options of jquery datepicker to instanciate new datepicker with same options?
如何获取 jquery datepicker 的所有选项以使用相同的选项实例化新的 datepicker?
I want clone a table which contains 2 datepickers with different options. You can see here an example : http://jsfiddle.net/qwZ5x/4/
我想克隆一个包含 2 个具有不同选项的日期选择器的表。你可以在这里看到一个例子:http: //jsfiddle.net/qwZ5x/4/
<div class="clonable">
<table><tr><td>
<input type="text" id="datepicker" />
<script>
jQuery(document).ready(function() {
jQuery("#datepicker").datepicker({
showOn: "both",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif"
});
});
</script></td><td>
<input type="text" id="datepicker2" />
<script>
jQuery(document).ready(function() {
jQuery("#datepicker2").datepicker();
});
</script>
</td></tr></table>
</div>
<a href="javascript:void(0);" id="clone">Clone</a>
var id = 0;
jQuery("#clone").on("click", function () {
var clonable = jQuery(".clonable:first").clone(true, true);
jQuery(clonable).find("input").each(function () {
jQuery(this).attr("id", jQuery(this).attr("id") + "_" + id);
jQuery(this).siblings('.ui-datepicker-trigger,.ui-datepicker-apply').remove();
jQuery(this).removeClass('hasDatepicker');
jQuery(this).datepicker({
showOn: "both",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif"
});
});
jQuery(clonable).insertBefore("#clone");
id = id + 1;
});
How can i set all options to cloned datepicker without set option one by one?
如何在没有设置选项的情况下将所有选项设置为克隆日期选择器?
Thank you very much.
非常感谢你。
回答by Travesty3
You can get the options of the existing datepicker using this:
您可以使用以下命令获取现有日期选择器的选项:
var options = jquery('#datepicker').datepicker('option', 'all');
to get one specific option, for example the numberOfMonths:
获得一个特定选项,例如 numberOfMonths:
var numberOfMonths = jquery('#datepicker').datepicker('option', 'numberOfMonths');
回答by DigitalDan
In your particular case, replace this:
在您的特定情况下,请替换以下内容:
jQuery(this).datepicker({
showOn: "both",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif"
});
...with this:
...有了这个:
jQuery(this).datepicker(jQuery(this).datepicker('option', 'all'));
Here's a jsfiddle with the change: http://jsfiddle.net/puAde/
这是一个带有变化的 jsfiddle:http: //jsfiddle.net/puAde/
This works because you've cloned the input elements, which hold the options, but they need to be re-applied to the new instance.
这是有效的,因为您已经克隆了包含选项的输入元素,但它们需要重新应用于新实例。
Note that you need to pass 'all'
to get the current options (the jQuery documentation is wrong).
请注意,您需要通过'all'
才能获取当前选项(jQuery 文档是错误的)。