雖然我會建議您驗證日期服務器並進行修改如果可能的話,例如用戶可能沒有javascript,並且手動將日期放在那裏,無論如何您都應該嘗試將日期轉換爲日期(使用strtotime的php),並且您可以使用相同的塊將js時間戳轉換爲unix時間戳!您需要做的很多事情才能在客戶端做出這樣的防彈,因爲我會解釋一種情況,但回答您的問題並按照您的方式進行操作,這是一種開始的方式:
您需要創建一個單獨的輸入字段顯示jQuery UI的日期選擇器可以稱之爲
<input id='display_startdate' type='text' name='does-not-matter-server-will-not-use-this'>
,然後你的舊<input type='hidden' name='startdate' id='startdate'>
將被用於服務器端的代碼,但你不會在這一領域應用的jQuery日期選擇器。你可以把它作爲類型文本進行測試,然後將其更改爲隱藏,因爲用戶不需要看到這個!
$(function(){
$("#display_startdate").datepicker({
altFormat: '@' ,
altField: '#Inspection_End',
//now the lines below will divide the ms by 1000 to give you POSIX TIMESTAMP
onSelect: function() {
fixTime();
}
});
});
//There are other times you need to evaluate the value of this field
//as an example some browsers will keep the input user entered on page refresh
//but will not keep the value javascript created
$('#yourForm').submit(function(){
//or $('#yourSubmit').click(function(){
fixTime();
//do rest!
});
function fixTime(){
$('#startdate').val($('#startdate').val()/1000);
}
來源
2011-05-25 18:04:59
Neo
我試圖從jquery.ui.datepicker.js中除以1000,但我不知道該在哪裏做。我試過的兩個地方都導致時間戳顯示負數。 – dallen 2010-08-04 18:48:02
嗯......你有最新版本的插件嗎?看來這個問題已經在4.0.0版本中得到了糾正 – aletzo 2010-08-04 21:15:02