jquery datepicker将click事件添加到prev next按钮

时间:2015-01-12 14:25:42

标签: jquery events datepicker click

我有这个函数,它将为给定的元素id设置一个日期选择器,并且我试图在prev和next按钮上添加一个click功能。但似乎没有任何效果。我在网上搜索了将点击事件添加到这些按钮,但它们都是id。

我在这里做错了什么?

function setDatepicker(element,curdate,identifier){
    //identifier is the element.id
    var minDateMaxDate = getCalendarsYearRange(1);
    var min = new Date(minDateMaxDate[0] * 1000);
    var max = new Date(minDateMaxDate[1] * 1000);

    $(element).datepicker({
          id: identifier,                 
          firstDay: 1,
          showOtherMonths: false,
          selectOtherMonths: false,
          changeYear: true,
          changeMonth: true,
          yearRange: getCalendarsYearRange(0),
          monthNamesShort:monthNamesShort,
          dayNamesShort:dayNamesShort,
          minDate: min,
          maxDate: max,
          onClose: function (dateText, inst){
            saveFieldsChanges(element,dateText,inst);
            getFieldsFromDialog();
          }
    });

    $(element).datepicker( "option", "dateFormat", "dd-mm-yy" );
    $(element).datepicker( "option", "defaultDate", curdate );  
    $("#"+identifier+" .ui-datepicker-div .ui-datepicker-header .ui-datepicker-prev").live('click', function(){     
        console.log("prev");
    });
    $("#"+identifier+" .ui-datepicker-div .ui-datepicker-header .ui-datepicker-next").live('click', function(){     
        console.log("next");
    });

}

更新

我设置了datepicker的Html,这是加载的xsl模板的一部分,输入字段是日期选择器。

<div class="ferias-form-div">

        <div class="labelfieldcontainer">
            <div class="formlabels">Data início</div>
            <div class="formfields">
                <input type="text" id="datainicioferias" onblur="saveFieldsChanges(this)" onkeyup="saveFieldsChanges(this)" name="datainicioferias" value="" class="textbox"/>
                <div class="errodialog" id="datainiciodialogferias"></div>
            </div>
        </div>


        <div class="labelfieldcontainer">
            <div class="formlabels">Data fim</div>
            <div class="formfields">
                <input type="text" id="datafinalferias" onblur="saveFieldsChanges(this)" onkeyup="saveFieldsChanges(this)" name="datafinalferias" value="" class="textbox"/>
                <div class="errodialog" id="datafimdialogferias"></div>
            </div>
        </div>
...

这就是我设置datepicker的方法:

setDatepicker(inputelement,curdate,"datafinalferias");

更新2 - 使用answear

Alexander 的帮助下,我将此选项添加到日历

onChangeMonthYear: function (year, month, inst ) {

            var limites;
            var cal_date_string = month+'/'+year;           
            limites = getCalendarsYearRange(2); //my calendar's navigation limits
            var end_date_string = limites[1]; //my calendar's navigation limits
            var start_date_string = limites[0]; //my calendar's navigation limits

            if(end_date_string == cal_date_string){         
                setTimeout( function(){ $(".ui-datepicker-next").attr( "onclick", 'mensagemLimite(1);' )},100);
            }

            if(start_date_string == cal_date_string){ 
                setTimeout( function(){ $(".ui-datepicker-prev").attr( "onclick", 'mensagemLimite(0);' )},100);
            }

          }

这样当prev / next按钮变为非活动状态时,它将获得我想要的onclick功能。

1 个答案:

答案 0 :(得分:11)

试试这个

$(document).on('click', '.ui-datepicker-next', function () {
  console.log('next');  
})

$(document).on('click', '.ui-datepicker-prev', function () {
  console.log('prev');  
})