jquery datepicker - 覆盖数据属性中的选项

发布于 2024-12-18 13:00:43 字数 774 浏览 4 评论 0原文

我正在使用这个标记,

<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 }; 

但是我如何添加/覆盖数据属性中的值..我只是不知道

I'm using this markup

<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>

I don't really know where to start with that options object..
Starting with the default values seems like a good start

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

But then how I add/overwrite with values from the data-attribute .. I just don't know

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-12-25 13:00:43

使用此标记(您必须像这样格式化“数据”属性,以便将它们识别为对象):

<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/

With this markup (you must format the "data" attribute like this so that they are recognized as objects):

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

You could do:

$('[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);
});

fiddle here: http://jsfiddle.net/WrRte/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文