2016-12-02 44 views
0

我試圖修改一個現有的datepicker插件(Pikaday)以顯示選定的日期在一些呈現的HTML中(這是呈現在插件中)。JavaScript:如何從沒有`return'的方法檢索值

我已經確定下面的setDate方法/函數返回我想要使用的日期字符串,但我不確定如何提取它並在我的函數中使用它。

的方法的setDate顯示爲:

setDate: function(date, preventOnSelect) 
    { 
     if (!date) { 
      this._d = null; 

      if (this._o.field) { 
       this._o.field.value = ''; 
       fireEvent(this._o.field, 'change', { firedBy: this }); 
      } 

      return this.draw(); 
     } 
     if (typeof date === 'string') { 
      date = new Date(Date.parse(date)); 
     } 
     if (!isDate(date)) { 
      return; 
     } 

     var min = this._o.minDate, 
      max = this._o.maxDate; 

     if (isDate(min) && date < min) { 
      date = min; 
     } else if (isDate(max) && date > max) { 
      date = max; 
     } 

     this._d = new Date(date.getTime()); 
     setToStartOfDay(this._d); 
     this.gotoDate(this._d); 

     if (this._o.field) { 
      this._o.field.value = this.toString(); 
      fireEvent(this._o.field, 'change', { firedBy: this }); 
      //I want to return `this._o.field.value` 
     } 
     if (!preventOnSelect && typeof this._o.onSelect === 'function') { 
      this._o.onSelect.call(this, this.getDate()); 
     } 
    } 

,我想回到this._o.field.value

有人會知道我怎麼能抓住這個並在我的功能中使用它? PS,我來自一個PHP OOP背景,所以我對JS OOP的理解可能會缺乏,對不起。我的HTML渲染功能:

renderHeading = function(instance, c, year, month, days) 
{ 
    var opts = instance._o; 
    var html = '<div class="pika-heading">'; 
    html += '<h3 class="pika-heading-year">' + year + '</h3>'; 
    html += '<h1 class="pika-heading-date">' + opts.i18n.weekdaysShort[day] + ', ' + opts.i18n.monthsShort[month] + '</h1>'; 
    return html += '</div>'; 
} 
+0

我會在'如果(this._o.field)'已經寫了像'window.myDateProperty'並在PHP讀取。 –

回答

0
if (this._o.field) { 
      this._o.field.value = this.toString(); 
      window.myDateProperty = this._o.field.value;// Add your own property 
      fireEvent(this._o.field, 'change', { firedBy: this }); 
      //I want to return `this._o.field.value`-- I wont prefer returning as that alters the library code. 
     } 

將現包含this._o.field.valuewindow.myDateProperty將在javascript文件可用。

PS:永遠做檢查:

if(window.myDateProperty){ 
    //use window.myDateProperty 
} 
+0

如果有多個元素連接到setDate回調,這將不起作用。如果你想這樣做,你必須提供當前字段的上下文,同時將該值存儲爲全局變量。像'window [this._o.field.fieldName] = this._o.field.value;' – dpkg

相關問題