javascript - jQuery UI datepicker options as variable - Stack Overflow

I have a number of inputs on a page function as JQ UI datepicker. Each needs to have different settings

I have a number of inputs on a page function as JQ UI datepicker. Each needs to have different settings. I want to minimize the JS so I save the settings as an attribute in each individual .

<input type="text" class="datepicker" name="dateofbirth" id="dateofbirth" size="20" value="" options="{ dateFormat: 'yy-mm-dd',changeYear: true,yearRange: '1920:2010'}" />

<input type="text" class="datepicker" name="expdate" id="expdate" size="20" value="" options="{ yearRange: '2011:2020'}" />

I use js to load the options dynamically as the settings.

    $(document).ready(function(){
 $(".datepicker").each(function(index){
  $(this).datepicker("option" , $(this).attr('options'));
 });
});

datepicker is not functioning. If I empty the parentheses after $this.datepicker it works fine.

I have also tried another way to assign settings. ("option",...) no dice.

I have a number of inputs on a page function as JQ UI datepicker. Each needs to have different settings. I want to minimize the JS so I save the settings as an attribute in each individual .

<input type="text" class="datepicker" name="dateofbirth" id="dateofbirth" size="20" value="" options="{ dateFormat: 'yy-mm-dd',changeYear: true,yearRange: '1920:2010'}" />

<input type="text" class="datepicker" name="expdate" id="expdate" size="20" value="" options="{ yearRange: '2011:2020'}" />

I use js to load the options dynamically as the settings.

    $(document).ready(function(){
 $(".datepicker").each(function(index){
  $(this).datepicker("option" , $(this).attr('options'));
 });
});

datepicker is not functioning. If I empty the parentheses after $this.datepicker it works fine.

I have also tried another way to assign settings. ("option",...) no dice.

Share Improve this question edited Sep 15, 2017 at 12:18 anatol 7879 silver badges16 bronze badges asked Jan 6, 2011 at 3:29 Desmond LiangDesmond Liang 2,3104 gold badges26 silver badges33 bronze badges 2
  • In fact, the specification w3c attribute options do not exist, you tried to substitute other attributes, such as @ title or @ alt, which unfortunately is also not used in the tags input? – Zhasulan Berdibekov Commented Jan 6, 2011 at 3:56
  • 1 man.. if it does not exist that doesnt mean you cant add it. adding the attr is perfectly fine there.. – Baz1nga Commented Jan 6, 2011 at 4:00
Add a ment  | 

3 Answers 3

Reset to default 2

Try this

$(this).datepicker(eval('(' + $(this).attr('options') + ')'));

Or use a json parser http://www.json/js.html

essentially your are doing this:

$(document).ready(function(){
    $(".datepicker").each(function(index){
        $(this).datepicker("option" , "{ dateFormat: 'yy-mm-dd',changeYear: true,yearRange: '1920:2010'}" );
    });
});

when you need to be doing either

$(document).ready(function(){
    $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd',changeYear: true,yearRange: '1920:2010'});
});

or this

$(document).ready(function(){
    $(".datepicker").datepicker();
    $(".datepicker").datepicker("option" , "dateFormat", 'yy-mm-dd');
    $(".datepicker").datepicker("option" , "changeYear", true);
    $(".datepicker").datepicker("option" , "yearRange", '1920:2010');
});

The first solution initiates the datepicker with a set of options, while the second solution initiates a datepicker, and then sets individual options. Your version was trying to be some hybrid of the two, and tried to use a string instead of an object literal encased in curly brackets.

Instead of using eval (which is evil, btw) you can use $.parseJSON like this:

$(this).datepicker( "option" , $.parseJSON( $(this).attr('options')) );

However, you should be careful to have valid JSON string.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744683669a4587784.html

相关推荐

  • javascript - jQuery UI datepicker options as variable - Stack Overflow

    I have a number of inputs on a page function as JQ UI datepicker. Each needs to have different settings

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信