2014-06-12 120 views
1

我有一個日曆按鈕,點擊打開日期選擇器並將日期放入輸入文本框中。我想要發生的事情是,第二個文本框會自動填入日期爲未來30天的日期。我遇到的麻煩是讓它與jquery協同工作。在jquery中執行第二次點擊功能

HTML:

<tr> 
<td align = "center">Entry Date From: <input id="ENTRYDATEFROM" name="ENTRYDATEFROM" type="text" maxLength="10" size="12" value=""> 
<img height="20"src="calendarsrc" id="entrySrc"></td> 
<td align = "center">Entry Date To: <input id="ENTRYDATETO" name="ENTRYDATETO" type="text" maxLength="10" size="12" value=""> 
<img height="20"src="calendarsrc" id="entrySrc2"></td> 
</tr> 

JQUERY:

$(document).ready(function() { 

    $("#entrySrc").click(function(){ 
    gAnytime.fPopCalendar(document.myform.ENTRYDATEFROM); 
    }); 

    $("#entrySrc2").click(function(){ 
    gAnytime.fPopCalendar(document.myform.ENTRYDATETO); 
    }); 

    //Tried this but had no success 
    //$(document).on("change", "#entrySrc", populate); 


}); 

function populate(){ 
var q = $("#ENTRYDATEFROM"); 
var dateTo = new Date(q.val()); 
var newDate = new Date(dateTo.setDate(dateTo.getDate() + 30)); 
var formatted = padNumber(newDate.getUTCMonth() + 1) + '-' + padNumber(newDate.getUTCDate()) + '-' + newDate.getUTCFullYear(); 
$("#ENTRYDATETO").val(formatted); 
} 

function padNumber(number) { 
    var string = '' + number; 
    string  = string.length < 2 ? '0' + string : string; 
    return string; 
} 

這是我的GUI是什麼樣子點擊任何東西之前:

這是當我點擊發生了什麼位於inp右側的#entrySrc日曆按鈕UT文本框中

然後我就可以點擊我希望日曆框內的任何日期。這會將輸入文本框填充到它的左側。

如何在日曆框中的第二次點擊執行我的填充功能?

+0

我已經甚至走得更遠,做一個鼠標鬆開鼠標按下和....但仍沒有:( –

+0

怎麼可能我正在努力解決我的問題? –

+0

我正在努力解決您的點擊處理程序正在嘗試完成的任務...您能否在JSFiddle中提供您的HTML(使用您的代碼)? –

回答

2

可能有一個非常簡單的解決方案:只需在#entrySrc 更改時觸發populate()方法。

$(document).on("change", "#entrySrc", populate); 

或這些替代品之一:你傳遞populate,不populate()

$("#entrySrc").on("change", populate); 
$("#entrySrc").change(populate); 

音符。

+0

沒有收到結果,#entrySrc實際上是日曆按鈕圖像,我也試過: '$(「#ENTR (「change」,populate());' 每當輸入文本框發生變化時,都會調用它,但沒有任何內容 –

+0

我想確保您沒有在您的系統中使用緩存代碼試驗。你難以清醒嗎? Windows上的Ctrl + F5,Mac上的Cmd + Shift + R。對舊代碼進行測試是我在工作中總要注意的地方:) –

+0

哦,我看到了問題。你不能像這樣調用'populate()',你必須省略括號。我沒有輸錯。 –

1

基於非常差勁的文檔在這裏:http://calendarxp.net/tutorials/flat/tutorials/PluginsSDK.htm我猜你需要做到以下幾點:

打開plugins.js文件,這顯然是其中掛鉤到的全局函數的負載(這種控制是sooooooo老)。

把你的代碼放到fOnChange模板(我收集將是一個幾乎空函數):

///////////// Calendar Onchange Handler //////////////////////////// 
// It's triggered whenever the calendar gets changed to y(ear),m(onth),d(ay) 
// d = 0 means the calendar is about to switch to the month of (y,m); 
// d > 0 means a specific date [y,m,d] is about to be selected. 
// e is a reference to the triggering event object 
// Return a true value will cancel the change action. 
// NOTE: DO NOT define this handler unless you really need to use it. 
//////////////////////////////////////////////////////////////////// 
function fOnChange(y,m,d,e) { 
    .... put your code here .... 
return false; // return true to cancel the change. 
} 

你把什麼應該有實際用途的東西。我建議生成這樣的自定義事件:

function fOnChange(y,m,d,e) { 
    var $e = $(e.target); // (or e.originalTarget or whatever you can find with a debugger!) 
    $e.trigger("calchange"); 
    return false; // return true to cancel the change. 
} 

這將要求jQuery包含在他們的js文件之前。

在代碼中,聽像這樣爲所有日曆

$(document).on('calchange', populate);