简体   繁体   English

使用数据属性在jQueryUI日历上设置选项

[英]Using a data-attribute to set an option on jQueryUI calender

I'd like to use a "data-dateformat" attribute to set the dateformat option of a jQueryUI Datepicker 我想使用“ data-dateformat”属性设置jQueryUI Datepicker的dateformat选项

so I can do this on my page 所以我可以在我的页面上做

<input name="t1" type="text" class="datepicker" data-dateformat="MM YY"> 
<input name="t2" type="text" class="datepicker" data-dateformat="DD MM y">

and the datepicker will use the correct format 并且日期选择器将使用正确的格式

I tried 我试过了

 $( ".datepicker" ).datepicker({
                  dateFormat: $(self).attr('data-dateformat') 
               });

but that doesn't work. 但这不起作用。

Is there some way to do that ? 有什么办法吗?

Your self value will not have the proper scope for individual elements. 您的self价值将不会适合各个元素。 You need to loop over each datepicker. 您需要遍历每个日期选择器。

Try this: 尝试这个:

$(".datepicker").each(function(){
    var $this = $(this);
    $this.datepicker({
       dateFormat: $this.attr('data-dateformat')
    });
});

You can also use the jQuery data method. 您也可以使用jQuery data方法。

$(".datepicker").each(function(){
    var $this = $(this);
    $this.datepicker({
       dateFormat: $this.data('dateformat')
    });
});

you can try this may work 你可以尝试这可能会工作

$( ".datepicker" ).datepicker({
              dateFormat: $(this).data('dateformat') 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM