jQuery datepicker adding custom button

2019-01-19 07:27发布

问题:

I'm trying to add a custom button to the button panel of jQuery's datepicker. When I apply the following code though nothing happens:

 $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        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">Clear</button>');  
            btn.unbind("click").bind("click", function () {  
                $.datepicker._clearDate( input );  
            });  

            btn.appendTo( buttonPane );  

            }, 1 );  
        } ,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });

Any help would be appreciated.

回答1:

$(function(){
    var pickerOpts = {
        showOn: "button"
    };  
    $("#date").datepicker(pickerOpts);
});


回答2:

A related question "How do you add buttons to a jQuery datepicker in the button panel?" was answered in another post. For an example of how a "Prev Day" and "Next Day" button was added to the button panel, see How do you add buttons to a jQuery datepicker in the button panel?