我正在尝试在模态框中显示搜索结果(使用Django搜索)。希望当用户单击“搜索”按钮时,将弹出一个“模态”框,结果将显示在“模态”框中。现在,单击“搜索”按钮后,将在当前URL之后添加“?q = XXX ”,并且在页面仍在加载新页面时,将显示带有结果的模式框。网址。但是,页面完成加载新URL后,“模式”框将消失。为什么会发生这种情况,我该如何解决?预先感谢。
HTML模板
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel='stylesheet' href="{% static 'css/inText_Search.css' %}" />
...
...
<form method="GET" action="">
<input type="text" value = '{{ request.GET.q }}' name ='q' placeholder="Search within this manuscript..." style="display:inline-block; width:55%">
<button class="btn btn-success" type="submit" data-toggle="modal" data-target="#searchModal"> search </button>
</form>
<div class="modal fade" id="searchModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<span class="close" data-dismiss="modal">×</span>
<p><font color = 'red'>{{ request.GET.q }}</font> found on:
<ul>
{% for found_page in search_result %}
<script type=text/javascript>
var found_page_number='{{found_page.id_tei}}'
var found_page_number = found_page_number.slice(-3)
console.log('found on');
console.log(found_page_number);
</script>
<li>
<a href="/page/{{found_page.id_tei}}">
Page
<script type='text/javascript'>
document.write(found_page_number)
</script>
</a>
</li>
{% endfor %}
</ul>
</p>
</div>
</div>
</div>
views.py进行搜索:
def pageinfo(request,id):
...
query = request.GET.get("q")
search_result=[]
if query:
search_result = possible_pages.filter(fulltext__icontains=query)
template_name='viewpage.html'
return render(request,template_name, {'search_result': search_result, ...other arguments})