2011年12月25日 星期日

[jQuery] 在datepicker建立清除(clear)按鈕

Ref: http://bugs.jqueryui.com/ticket/3999

$(function () {
    //wrap up the redraw function with our new shiz
    var dpFunc = $.datepicker._generateHTML; //record the original
    $.datepicker._generateHTML = function (inst) {
        var thishtml = $(dpFunc.call($.datepicker, inst)); //call the original
        thishtml = $('<div />').append(thishtml); //add a wrapper div for jQuery context

        //locate the button panel and add our button - with a custom css class.
        $('.ui-datepicker-buttonpane', thishtml).append(
            $('<button class="\
                ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all\
                "\>清除</button>'
            ).click(function () {
                inst.input.attr('value', '');
                inst.input.datepicker('hide');
            })
        );

        thishtml = thishtml.children(); //remove the wrapper div
        return thishtml; //assume okay to return a jQuery
    };
});