2012-08-08 69 views
0

我的jQuery看起來是這樣的:動態JQuery的觀點

$('#id_start_date_list').change(
     function get_time() 
     { 
      var value = $(this).attr('value'); 
      alert(value); 
      var request = $.ajax({ 
       url: "/getTime/", 
       type: "GET", 
       data: {start_date : value},   
       dataType: "json", 
       success: function(data) {    
       //Popluate combo here by unpacking the json 
     } 
     }); 

     }); 

我view.py看起來是這樣的:

def getTime(request): 
    if request.method == "GET": 
     date_val = request.GET.get('start_date')       
     format = '%Y-%m-%d' 
     sd = datetime.datetime.strptime(date_val, format) 
     sql_qw = MeasurementTest.objects.filter(start_date = sd)   
     results = [{'start_time': str(date.start_time), 'id_start_time':date.start_time} for date in sql_qw] 
     print results     
     *****json_serializer = serializers.get_serializer("json")() 
     response_var= json_serializer.serialize(results, ensure_ascii=False, indent=2, use_natural_keys=True)***** 

    return HttpResponse(response_var, mimetype="application/json") 

我的html頁面看起來像這樣:

html> 
<head> 
    <title>date Comparison</title> 

<script src="http://code.jquery.com/jquery-latest.js"></script>  
</head> 
<body> 
    {% if form.errors %} 
     <p style="color: red;"> 
      Please correct the error{{ form.errors|pluralize }} below. 
     </p> 
    {% endif %} 
    <form action="/example/" method="post" align= "center">{% csrf_token %} 
     <table align = "center"> 
     <tr> 
      <th><label for="start_date_list">Date 1:</label></th> 
      <th>  {{ form.start_date_list }}   </th>    
     </tr>     
     <tr> 

     <th><label for="start_time_list">time:</label></th> 
     <th><select name="start_time_list" id="start_time_list"></th> 
      <option></option>   
     </select> 
     <th></th> 

     <div id = "log"></div> 
     </tr> 

     <tr align = "center"><th><input type="submit" value="Submit"></th></tr> 
     </table>   
    </form> 
</body> 
</html> 

正如你所看到的,我從選擇框中獲取值,並且正在對數據庫執行操作並將值存儲在json對象中。

有兩個部分,我完全失明。 首先是json對象,我不確定結果是否存儲在response_var對象中。 第二個是,我不知道如何從json對象獲取值到新的「start_time_list」列表中

詳細說明:我在json對象初始化過程中做了什麼錯誤。我試圖打印respose_var,但似乎不打印在控制檯上。我使用正確的語法嗎?並可以有人告訴我如何查看存儲在JSON對象中的值view.py

以類似的方式,我如何執行操作jQuery的一面,從json對象提取值以及如何分配通過示例代碼和可能的解決方案將json對象的值添加到列表框中。

回答

0

要將結果轉換成JSON,使用simplejson

from django.utils import simplejson 
def getTime(request): 
if request.method == "GET": 
    date_val = request.GET.get('start_date')       
    format = '%Y-%m-%d' 
    sd = datetime.datetime.strptime(date_val, format) 
    sql_qw = MeasurementTest.objects.filter(start_date = sd)   
    results = [{'start_time': str(date.start_time), 'id_start_time':date.start_time} for date in sql_qw] 
    print results 
    response_var = simplejson.dumps(results) 

return HttpResponse(response_var, mimetype="application/json") 

要訪問JSON對象在JavaScript,看看你的Ajax請求。在這種情況下,成功回調正在傳遞參數data。這是包含服務器響應的變量。所以,要訪問合成陣列(例如)的第一個元素,你可以:

var request = $.ajax({ 
    url: "/getTime/", 
    type: "GET", 
    data: {start_date : value},   
    dataType: "json", 
    success: function(data) {    
    //Popluate combo here by unpacking the json 
    data[0]; // This is the first element of the array sent by the server 
    } 
}); 

最後,修改HTML,jQuery提供很多方法,如htmlappend。值得看看doc。因此,如果要爲選擇標記構建選項集合,則應使用javascript for循環,jQuery each方法或類似方法迭代結果集,構建選項(這可以通過連接字符串或創建DOM元素,我認爲這是最好的解決方案,因爲它執行得更好),並使用前面提到的方法之一將它們插入到html中。