为所有日期选择器设置属性,也可以单独设置属性
假设您有一个包含多个日期选择器的页面,并且您想要将一些具有相同值的属性设置为所有这些属性(使用类选择器),然后将非共享属性设置为其中每个属性(使用id 选择器)。如何做到这一点?
这是我到目前为止所尝试的
$(document).ready(function() {
// Shared properties for all datepickers with class="datepicker"
$( ".datepicker" ).datepicker({
showOn: 'focus',
showButtonPanel: true,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: "d M yy"
});
// Specific properties for each datepicker
$("#myDate1").datepicker({
altFormat: "yy-mm-dd",
altField: "#myDate1_alt"
});
$("#myDate2").datepicker({
altFormat: "yy-mm-dd",
altField: "#myDate2_alt"
});
});
标记
<input type='text' id='myDate1' class="datepicker"/>
<input type="hidden" id="myDate1_alt"/>
<input type='text' id='myDate2' class="datepicker"/>
<input type="hidden" id="myDate2_alt"/>
它很好地设置了共享属性,但没有设置特定的属性。我还尝试颠倒顺序(“#myDate1”-“#myDate2”-“.datepicker”,但“.datepicker”的属性消失了)。
Suppose you have a page with multiple datepickers and you want to set some properties with the same value to all of them (with a class selector), and then set non shared properties to each one of them (with an id selector). How to do this?
Here is what I´ve tried so far
$(document).ready(function() {
// Shared properties for all datepickers with class="datepicker"
$( ".datepicker" ).datepicker({
showOn: 'focus',
showButtonPanel: true,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: "d M yy"
});
// Specific properties for each datepicker
$("#myDate1").datepicker({
altFormat: "yy-mm-dd",
altField: "#myDate1_alt"
});
$("#myDate2").datepicker({
altFormat: "yy-mm-dd",
altField: "#myDate2_alt"
});
});
Markup
<input type='text' id='myDate1' class="datepicker"/>
<input type="hidden" id="myDate1_alt"/>
<input type='text' id='myDate2' class="datepicker"/>
<input type="hidden" id="myDate2_alt"/>
It sets well the shared properties but not the specific ones. I also tried inverting the order ("#myDate1" - "#myDate2" - ".datepicker", but then the properties for ".datepicker" were gone).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
option
动词传递给datepicker()
方法:You can pass the
option
verb to thedatepicker()
method:它也应该是带有大写 p 的
datePicker
:当我更改为
datePicker
时,您的代码可以工作。It should also be
datePicker
with a capital p:When I changed to
datePicker
, your code worked.