2015-01-12 110 views
4

我有這個功能,將爲給定的元素id設置日期選擇器,我試圖添加一個點擊功能prev和下一個按鈕。但似乎沒有任何工作。我已經搜索過網頁,爲這些按鈕添加點擊事件,但他們都提供了一個ID。jquery datepicker添加點擊事件prev next button

我在做什麼錯在這裏?

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

} 

更新

的Html其中i設定日期選擇器,這是在加載XSL模板的一部分,輸入字段是datepickers。

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

我這是怎麼設置例如日期選擇:

setDatepicker(inputelement,curdate,"datafinalferias"); 

更新2 - 隨着answear

具有利於從亞歷山大我加入這個選項日曆

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函數。

回答

11

試試這個

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

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