2012-04-19 55 views
0

我試圖用這個: http://trentrichardson.com/examples/timepicker/不工作jQuery用戶界面的DateTimePicker(node.js的+ EJS +快遞)

這是我的代碼:

<html> 
<head> 
    <script src="http://trentrichardson.com/examples/timepicker/js/jquery-1.7.1.min.js">  </script> 
    <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-1.8.16.custom.min.js"></script> 
    <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-sliderAccess.js"></script> 
    <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon.js"></script> 
    <link type="text/css" href="http://trentrichardson.com/examples/timepicker/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet"> 

<script> 

$(function() { 
    $('#dateTimePicker1').datetimepicker(); 
}); 

</script> 

</head> 
<body> 
    <%-body%>  
</body> 
</html> 

這就是body.ejs:

<input id="dateTimePicker1" class="hasDatepicker"></input> 

我得到的是一個簡單的輸入。當我點擊它時沒有任何反應。我在控制檯中沒有錯誤。我需要幫助解決這個問題。

UPD(我有一個路由文件):

exports.index = function(req, res){ 
    res.render('index', { title: 'index' }) 
}; 

我檢查,並輸入在頁面加載後的代碼。

但是,奇怪的是:我用作者的插件打開網站(本帖子中的第一個鏈接)。打開開發人員工具。編輯站點的html - 在示例輸入附近添加新的輸入。名稱(和id)它example222。在控制檯上做

$('#example222').datetimepicker(); 

他的輸入工作,我的不!那是什麼樣的魔法? :)

+0

你找到這個解決辦法嗎?我在相同的設置(node + ejs + express)中遇到同樣的問題 – Vinoth 2012-07-08 08:15:50

+0

找到了解決方案嗎?似乎有一個錯誤。 – spencerthayer 2014-10-17 07:13:24

回答

0

首先,我認爲你的腳本是在DOM被加載之前執行的,這是錯誤的,因爲它被包裝在$(...)中,並且應該像包裝在$ .ready(...)中一樣。

如果您沒有收到任何錯誤,表示代碼沒有找到與選擇器匹配的任何DOM元素,這意味着您的模板未應用。

您能否檢查一下您的源代碼(在瀏覽器中)是否有<input id="dateTimePicker1" class="hasDatepicker"></input>或仍然包含ejs模板。

那麼你能告訴我們如何在node.js中調用渲染嗎?

編輯:問題是沒有執行jQuery代碼。

這將工作由this jsFiddle作爲證明:

<html> 
    <head> 
     <script src="http://trentrichardson.com/examples/timepicker/js/jquery-1.7.1.min.js">  </script> 
     <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-1.8.16.custom.min.js"></script> 
     <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-sliderAccess.js"></script> 
     <script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon.js"></script> 
     <link type="text/css" href="http://trentrichardson.com/examples/timepicker/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet"> 
    </head> 
    <body> 
     <input id="dateTimePicker"></input> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#dateTimePicker').datetimepicker(); 
      }); 
     </script> 
    </body> 
</html> 
+0

我編輯了帖子。請重新閱讀。 – kulebyashik 2012-04-20 04:46:39

+0

我編輯了我的回覆,腳本代碼沒有執行。 – 2012-04-20 08:01:50

相關問題