douquan1953 2014-02-18 13:14
浏览 62
已采纳

添加按钮到datepicker + 6个月

i have a javascript code to create an extra button in the datepicker panel when clikced on the input filed.

$(function() {
$( ".datepicker" ).datepicker({ dateFormat: "yy-mm-dd", 
changeMonth: true,
changeYear: true,
yearRange: "2014:2034",
showButtonPanel: true,
    beforeShow: function (input) {
        setTimeout(function () {
            var buttonPane = $(input)
                .datepicker("widget")
                .find(".ui-datepicker-buttonpane");

            var btn = $('<button class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all" type="button">CSA</button>');
            btn.unbind("click")
            .bind("click", function () {
                //$.datepicker._clearDate(input);
                //alert('custom text');
                $(input).datepicker("hide");
                $(input).val("0000-00-00");
            });

            btn.appendTo(buttonPane);

        }, 1);
    }
});
});

Now when i click the extra button "CSA" in the panel, 0000-00-00 appears. Now what i want to is that when the button "CSA" is clicked the date off today + 6 months to appear. Is that possible in datepicker?

  • 写回答

3条回答 默认 最新

  • dongxianchu3541 2014-02-18 13:34
    关注

    I did this a while back adding year support to the calendar: https://stackoverflow.com/a/10558308/402706

    Without much tweaking you can get a +6 month (from today) button as well.

    enter image description here

    <kbd>Live Demo</kbd>

    The relevant portion of the change is in the _generateHTML method.

            var prev = (this._canAdjustMonth(inst, -1, drawYear, drawMonth) ?
            '<a style="left: 22px;" class="ui-datepicker-prev ui-corner-all" onclick="DP_jQuery_' + dpuuid +
            '.datepicker._adjustDate(\'#' + inst.id + '\', -' + stepMonths + ', \'M\');"' +
            ' title="' + prevText + '"><span class="ui-icon ui-icon-circle-triangle-' + (isRTL ? 'e' : 'w') + '">' + prevText + '</span></a>' :
            (hideIfNoPrevNext ? '' : '<a style="left: 22px;" class="ui-datepicker-prev ui-corner-all ui-state-disabled" title="' + prevText + '"><span class="ui-icon ui-icon-circle-triangle-' + (isRTL ? 'e' : 'w') + '">' + prevText + '</span></a>'));
    
            var nextText = this._get(inst, 'nextText');
            nextText = (!navigationAsDateFormat ? nextText : this.formatDate(nextText,
            this._daylightSavingAdjust(new Date(drawYear, drawMonth + stepMonths, 1)),
            this._getFormatConfig(inst)));
            var next = (this._canAdjustMonth(inst, +1, drawYear, drawMonth) ?
            '<a style="right: 22px;" class="ui-datepicker-next ui-corner-all" onclick="DP_jQuery_' + dpuuid +
            '.datepicker._adjustDate(\'#' + inst.id + '\', +' + stepMonths + ', \'M\');"' +
            ' title="' + nextText + '"><span class="ui-icon ui-icon-circle-triangle-' + (isRTL ? 'w' : 'e') + '">' + nextText + '</span></a>' :
            (hideIfNoPrevNext ? '' : '<a style="right: 22px;" class="ui-datepicker-next ui-corner-all ui-state-disabled" title="' + nextText + '"><span class="ui-icon ui-icon-circle-triangle-' + (isRTL ? 'w' : 'e') + '">' + nextText + '</span></a>'));
    
            next += (this._canAdjustMonth(inst, +1, drawYear, drawMonth) ?
            '<a class="ui-datepicker-next ui-corner-all" onclick="DP_jQuery_' + dpuuid +
            '.datepicker._setDateDatepicker($(\'#' + inst.id + '\')[0], new Date(new Date().setMonth(new Date().getMonth()+6)));"' +
            ' title="CSA"><span style="cursor:pointer;cursor:hand;">CSA</span></a>' :
            (hideIfNoPrevNext ? '' : '<a class="ui-datepicker-next ui-corner-all ui-state-disabled" title="CSA"><span style="cursor:pointer;cursor:hand;">CSA</span></a>'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。