2011-05-11 44 views
1

我有:jquery1.6.min,jquery-tmpl.js(最新測試版)和knockout-1.2.0.js。我使用了一個非常簡單的例子,但我無法獲取jQuery模板來渲染,我找不到原因,簡單地說,我可以通過使用螢火蟲掃描dom來查看dom中的元素 - 我有一些tmp = annonymous(jquery,$ item )出現,但數據不在dom內呈現。開始與jQuery模板問題

模板:

<script id="bookTemplate" type="text/x-jquery-tmpl"> 
    <h2>${title}</h2> 
    price: ${formatPrice(price)} 
</script> 

jscode:

<script type="text/javascript"> 
     $(document).ready(function() { 
     // Create an array of books 
     var books = [ 
      { title: "ASP.NET 4 Unleashed", price: 37.79 }, 
      { title: "ASP.NET MVC Unleashed", price: 44.99 }, 
      { title: "ASP.NET Kick Start", price: 4.00 }, 
      { title: "ASP.NET MVC Unleashed iPhone", price: 44.99}]; 

     function formatPrice(price) { 
      return "$" + price.toFixed(2); 
     } 

     // Render the books using the template 
     $('#bookTemplate').tmpl(books).appendTo('#bookContainer'); 
     }); 
    </script> 

回答

0
window.formatPrice = function formatPrice(price) { 
    return "$" + price.toFixed(2); 
} 

Live Example

你調用模板formatPrice但在你關閉定義的功能。爲了能夠做到這一點,formatPrice必須在全球範圍內。一個簡單的方法來做到這一點是分配功能windiw.formatPrice

+0

我覺得自己像一個完全白癡......你顯示我幫助了我,但我錯過了添加我追加的實際容器:'

' – Haroon 2011-05-11 16:15:17

+0

@哈龍哦。我認爲你有你的HTML:D如果它解決了你的問題接受它:) – Raynos 2011-05-11 16:19:09

+0

我不得不等待4分鐘出於某種原因,會做。謝謝 :) – Haroon 2011-05-11 16:20:02