2014-02-25 4 views
0

장고 양식이 있습니다. 필드 중 하나 (monitoring_method)는 다른 필드 (database_type)의 항목을 기반으로 결과를 필터링하는 자동 완료 라이트 위젯을 사용합니다. submit하기 전에 database_type 필드에 사용자가 입력 한 값을 가져 오는 방법이 있습니까? 나는 AJAX로 그것을하는 방법을 (또는 그것을 알아낼 수 있었다) 알았지 만, 나는 확실하지 않다 - 그리고 아마도 이것이 내 질문이다 - AJAX를 자동 완성과 통합하는 방법.제출하기 전에 양식 필드 값 받기

class MonitoringMethodAutocomplete(autocomplete_light.AutocompleteBase): 
    autocomplete_js_attributes = {'placeholder': 'Choose a database type to enable monitoring method selection'} 

    def choices_for_request(self): 
     q = self.request.GET.get('q', '') 
     db_type = self.request.POST.get('database_type') 
     # if not db_type: 
     #  return [] 
     monitoring_methods = Database.objects.values_list('monitoring_method', flat=True) 
     return monitoring_methods.filter(database_type__exact=db_type, 
             name__icontains=q).distinct() 
    def choices_for_values(self): 
     return [] 

편집 : 그래서, 나는 원래 그런 내가 할 수없는 것 일을하려고했지만, 나는이 q 변수가 비슷한 일을하고있는 것을 깨달았다 무슨 ... 왜 db_type가 작동하지 않는 생각?

답변

0

예.

<script type="text/javascript"> 
$(init); 

function init() { 
    var database_form = $('.asset-form'); 
    var db_type_field = $('#id_database_type'); 
    database_form.data('db_type', db_type_field.val()); 

    db_type_field.on('change', function() { 
    var db_type = db_type_field.val(); 
    console.log(db_type); 
    $('.autocomplete-light-text-widget').yourlabsAutocomplete().data = {'database_type':db_type}; 
    }); 

    // disable enter key from posting form 
    $('#id_database_type').keypress(function(event) { 
     if (event.which == 13) { 
      event.preventDefault(); 
     }; 
    }); 
} 

</script> 
관련 문제