admin管理员组

文章数量:1379395

I'd like to use a "data-dateformat" attribute to set the dateformat option of a jQueryUI Datepicker

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 ?

I'd like to use a "data-dateformat" attribute to set the dateformat option of a jQueryUI Datepicker

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 ?

Share Improve this question asked Nov 2, 2012 at 4:11 Jo ErlangJo Erlang 3277 silver badges15 bronze badges 1
  • 1 What is self? Where do you assign it? Are you expecting it to be the input element? – loganfsmyth Commented Nov 2, 2012 at 4:13
Add a ment  | 

2 Answers 2

Reset to default 8

Your self value will not have the proper scope for individual elements. 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.

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

you can try this may work

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

本文标签: javascriptUsing a dataattribute to set an option on jQueryUI calenderStack Overflow