2014-09-01 66 views
0

我是uisng句柄模板,通過ajax調用顯示數據庫狀態列表。 下面是我的代碼如何去。通過ajax調用顯示句柄模板中的數據庫狀態列表

listofplaces:function(){ 

$.ajax({ 
     url: 'getStates', 
     type: 'POST', 
     async: false, 
     data: {}, 
     success: function(response){ 

     var source = $('#showstateTemplate').html(); 
     var template = Handlebars.compile(source); 
     $('#statecheck').html(template(response));   


     }, 

    }); 




}, 

我的模板是如下

  <script id="showstateTemplate" type="text/template"> 
      <option value="">Select State</option> 
      <option value={{'state'}}> {{state}} </option> 


      </script> 

我從服務器獲取如下

 {"state":[{"state":"Bihar"},{"state":"Assam"},{"state":"Uttar Pradesh"},{"state":"Maharashtra"},{"state":"West Bengal"},{"state":"Andhra Pradesh"},{"state":"Madhya Pradesh"},{"state":"Tamil Nadu"},{"state":"Karnataka"},{"state":"Gujarat"},{"state":"Orissa"},{"state":"Kerala"},{"state":"Jharkhand"},{"state":"Punjab"},{"state":"Haryana"},{"state":"Chhattisgarh"},{"state":"Jammu and Kashmir"},{"state":"Uttarakhand"},{"state":"Himachal Pradesh"},{"state":"Tripura"},{"state":"Meghalaya"},{"state":"Manipur"},{"state":"Nagaland"},{"state":"Goa"},{"state":"Mizoram"},{"state":"Sikkim"},{"state":"Delhi"},{"state":"Pondicherry"},{"state":"Chandigarh"},{"state":"Dadra and Nagar Haveli"},{"state":"Lakshadweep"},{"state":"Arunachal Pradesh"},{"state":"Rajasthan"},{"state":"Daman and Diu"},{"state":"Andaman and Nicobar Islands"}]} 

誰能告訴我如何顯示在模板國家的名單響應? 在此先感謝。

回答

0

模板渲染是否沒有錯誤?如果你達到那一點,以下是你的模板應該如何:

<script id="showstateTemplate" type="text/template"> 
    <option value="">Select State</option> 
    {{#each state}} 
    <option value='{{state}}'>{{state}}</option> 
    {{/each}} 
</script> 
+0

謝謝你回答@ dc2。我設法得到這個工作。是的,每個幫手都是必需的。 'var obj = JSON.parse(response); \t \t var source = $('#showdstateTemplate')。html(); \t var template = Handlebars.compile(source); $('#statecheck')。html(template(obj));'得到它的工作 – dhruv 2014-09-01 22:09:53

+0

偉大和沒有probs。 – dthree 2014-09-02 00:39:18