9

我使用這個標記jQuery的日期選擇器 -

<label> Date <input type="text" data-datepicker="{maxDate: '+1d'}" /></label> 
<label> Another date <input type="text" data-datepicker="" /></label> 
<script> 
$('[data-datepicker]').each(function() { 
     // init the options var with some default values (dateFormat etc) 
     // that can be overridden by the data-datepicker values 
     // also, new values can be added to the options from data-datepicker 
     // such as in the above example "maxDate" 
     var options = TODO;  

     $(this).datepicker(options); 
}); 
</script> 

從數據屬性選項將覆蓋我真的不知道從哪裏開始與該選項對象.. 以默認值開始似乎是個好啓動

var options = { dateFormat: 'yy-mm-dd }; 

但後來我如何添加/覆蓋從數據屬性值的。我只是不知道

回答

12

有了這個標記(您必須格式化「數據」屬性這樣使他們被認定爲對象):

<label> Date <input type="text" data-datepicker='{"maxDate": "+1d"}' /></label> 
<label> Another date <input type="text" data-datepicker='{ "dateFormat": "dd-mm-yy"}' /></label> 

你可以這樣做:

$('[data-datepicker]').each(function() { 
    //standard options 
    var options = { dateFormat: "yy-mm-dd"}; 
    //additional options 
    var additionalOptions = $(this).data("datepicker"); 
    //merge the additional options into the standard options and override defaults 
    jQuery.extend(options, additionalOptions); 


     $(this).datepicker(options); 
}); 

小提琴這裏:http://jsfiddle.net/WrRte/

+0

謝謝您的幫助。 – Ivar