book/url.py
urlpatterns = [
url(r'^hero_seach$', views.hero_seach, name="hero_seach"),
]
book/hero_seach.html
<script src="/static/book/js/jquery-1.12.4.min.js"></script>
<script>
$(function(){
$("#hname").keyup(function(){
$.ajax({
"url":"{% url "book:hero_seach" %}",
"data":{
"hname":$(this).val().trim()
},
"success":function(data){
$("#show ul").html("")
if(data.ret.length!=0){
data.ret.forEach(function (value) {
$("#show ul").append("<li>"+value["hname"]+"</li>")
})
}else{
$("#show ul").append("<li>无</li>")
}
}
})
})
})
</script>
<body>
<div>
<input type="text" id="hname">
</div>
<div id="show">
<ul></ul>
</div>
</body>
book/views.py
@check_user
def hero_seach(request):
hname = request.GET.get("hname")
if hname:
if hname=="":
ret={"ret":[]}
return HttpResponse(json.dumps(ret,ensure_ascii=False),content_type="application/json;charset=utf-8")
hname_list = HeroInfo.objects.filter(hname__icontains=hname).values("hname","id")
info = []
for i in hname_list:
d={}
d["hname"]=i["hname"]
d["hid"]=i["id"]
info.append(d)
ret={"ret":info}
return HttpResponse(json.dumps(ret,ensure_ascii=False),content_type="application/json;charset=utf-8")
return render(request, "book/hero_seach.html")
网友评论