2016-10-27 33 views
-1

我有一張從python django數據庫提取數據的表第一列是與產品名稱對應的選擇框我希望根據產品選擇更改價格列的值我如何做到這一點基於選擇框索引的Python Django更改值

<table> 
{% for item in items %} 
    <tr> 


<select name="sel0" id="selectbox" onchange=""> 
      {% for product in products %} 
      <option value="{{ product.id }}" id="optionval">{{product.name}}</option>        
      {% endfor %} 
     </select> 
     <td>price</td> 
     <td>Quantity</td> 
     <td>Total</td> 
    </tr> 
    {% endfor%} 


</table> 

回答

0

最好的辦法是寫產品列表的變化事件的ajax調用。 應顯示默認選定產品的裝載價格。 On產品更改從數據庫檢索價格並更新前端中的相應價格值。

$(document).on('change', '#selectbox', function(){ 
    $.ajax({ 
     type: "POST", 
     url: Urls['url_to_view']({{ product.id }}), 
     data:{ 
      csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').val() 
     }, 
     success: function(response){ 
      price = response.price 
      // code to change price in front end 
     } 
    }) 
});