首页 > 解决方案 > Ajax 选择下拉值并通过

问题描述

我正在尝试执行以下操作:用户将从下拉菜单中选择值,我想捕获数据并传递给值以检查它是否存在并过滤它。

使用 django 视图获取媒体数据文件列表。我想根据选定的数据站点进行过滤。

看法

@ajax_request
def listofiles(request, *args, **kwargs):
    dir_name = "configurations"
    template_name = 'loadlistofiles.html'
    path = os.path.join(settings.MEDIA_ROOT, dir_name)
    filelist = []
    context = {'file': filelist}

    for f in os.listdir(path):
        if f.endswith(".pdf"): # to avoid other files
            filelist.append("{}{}/{}".format(settings.MEDIA_URL, dir_name, f))
    return render(request, template_name, {'file': filelist})

上面的代码将提供所有文件的列表。我想使用下拉选择过滤它

HTML AJAX

<script>
$(document).ready(function() {
    $("#files").submit(function() { // catch the form's submit event
        $.ajax({
            url: 'loadlistofiles.html',
            type: $(this).attr('GET'),
            data: $(this).serialize(), // get the form data
            success: function(data) { // on success..
                $("#fetchdata").html(data); // update the DIV
            }
        });
    return false;
    });
});
</script>

如何根据下拉选择值过滤并检查文件中的字符串。例如: 文件名:dublin.aus1.pdf Dropdown 会有一个类似的列表:AUS1, CH1, IND1 喜欢检查是否aus1在文件中,如果是则只打印显示。

提前致谢。

标签: djangoajaxjinja2dropdown

解决方案


推荐阅读