django + jquery 实现二级联动

二级联动用ajax还是很好实现的,下面简单给个例子

jquery代码

$("#id_sel").change(function(){
    $.get("/browser/gettestcase?type="+cate, function(result){
        //$(".form-control-select").find('[value=header]').remove();
        //$(".form-control-select").selectpicker('refresh');
        if(result.length > 0) {
             $.each(result,function(index,value){
                 $(".form-control-select").append("<option value='"+value.label+"'>"+value.text+"</option>");
             })
        }
    }
)})

django代码

def gettestcase(request):
    options_list = []
    cate = request.GET.get('type', 'shop')
    print cate
    options = TestCase.objects.filter(case_category=cate)
    for case in options:
        c = {}
        c['label'] = case.case_name
        c['text'] = case.case_name
        options_list.append(c)      
    return HttpResponse(json.dumps(options_list), content_type='application/json')

 

posted @ 2016-05-19 15:04  晶莹的营  阅读(958)  评论(0编辑  收藏  举报