2011-10-10 74 views
2

我可能在這裏錯過了一些簡單的東西,但我遇到了jquery ui datepicker的麻煩。它似乎只是返回一個通用的對象,而不是一個Date對象,這不是我所期望的。下面是代碼:jquery ui datepicker getDate返回對象而不是日期

<!DOCTYPE HTML> 
<html> 
<head> 
    <meta charset="utf-8"> 

    <link type="text/css" href="jquery_ui/css/custom-theme/jquery-ui-1.8.14.custom.css" rel="stylesheet" />  
    <script type="text/javascript" src="jquery_ui/js/jquery-1.6.2.min.js"></script> 
    <script type="text/javascript" src="jquery_ui/js/jquery-ui-1.8.16.custom.min.js"></script> 
    <script type="text/javascript"> 

    Date.prototype.addHours= function(h){ 
     this.setHours(this.getHours()+h); 
     return this; 
    } 

    $(function() { 

     $("#startdatetime").datepicker( 
      { 
       onSelect: function(dateText, inst) { 
        sd = $("startdatetime").datepicker('getDate'); 
        sd.addHours(10); 
       } 
      }); 

    }); 
    </script> 
</head> 

<body > 
    <input id="startdatetime" type="text"> 
</body> 
</html> 

我得到的例外是「sd.addHours不是一個函數」,並使用螢火蟲看起來GETDATE是給我錯了對象(不是日期)。

我不是很熟悉javascript或jquery,有誰知道爲什麼會發生這種情況?

回答

4

你錯過了內部jQuery表達式中的井號。在任何情況下,實際上都不需要再次定位對象,因爲實例以inst的形式傳遞給函數。

+0

啊哈,我知道這是愚蠢的東西那樣。謝謝。 – WildCrustacean

1

addHour原生JavaScript Date類中有方法(請參見http://www.w3schools.com/jsref/jsref_obj_date.asp)。

你可以做的是下面這段代碼:

Date.prototype.addHours = function(amount) { 
return new Date(this.getTime() + (amount * 60 * 60 * 1000)); 
} 

$("#datepicker").datepicker("getDate").addHours(10);