Django的jQuery的的UI自动完成URL变化 [英] Django jquery-ui autocomplete url changing
问题描述
我使用的是在输入了jQuery的用户界面自动完成。它可以很好地用于自动填充,但是当我选择我输入的完整版本,并按下回车键,它关系到一个链接如 /?N =东西
我想改变这个链接 / baslik /东西
就应该到 / baslik /东西
每个输入。
我有这些在我的模板文件:
I'm using the jquery-ui autocomplete in an input. It works well for autocompleting but when I select the complete version of my input and hit enter, it goes to a link like /?n=something
I want to change this link to /baslik/something
it should go to /baslik/something
for each input.
I have these in my template file:
<script>
$(document).ready(function(){
$( "input#n" ).autocomplete({
source: "{% url "autoco" %}",
minLength: 2
});
});
</script>
<div class="ui-widget">
<input id="n" type="text" name="n"/>
</div>
<script type="text/javascript">
document.getElementById("n").addEventListener("change", function(){
this.form.setAttribute("action", "/baslik/" + this.value);
});
</script>
在code最后JS部分上面时,用户点击进入到 / baslik /东西
,但它只是做到这一点,如果我不改变输入从自动完成更改输入。
The last js part of the code above is transforming the input when user hits to enter to /baslik/something
but it just does this if I do not change the input from autocomplete.
视图:
def autoco(request):
term = request.GET.get('term')
bslk = Baslik.objects.filter(title__istartswith=term)
res = []
for b in bslk:
dict = {'id':b.id, 'label':b.__unicode__(), 'value':b.__unicode__()}
res.append(dict)
return HttpResponse(simplejson.dumps(res))
的网址:
url(r'^autoco/$', autoco, name='autoco'),
我怎样才能让我的方式这项工作?谢谢你。
How can I make this work in my way? Thanks.
推荐答案
有一个解决方案。
这是jQuery的的UI自动完成的JS部分:
There is a solution. This is js part of jquery-ui autocomplete:
<script>
$(document).ready(function(){
$( "input#n" ).autocomplete({
source: "{% url "autoco" %}",
minLength: 2,
select:function(e,ui) {
location.href = ui.item.the_link;
// console.log(ui.item.the_link);
}
});
});
</script>
我们加入the_link属性它,我们应该描述它在我们的观点:
这一部分:
We added the_link attribute to it and we should describe it in our view: this part:
dict = {'id':b.id, 'label':b.__unicode__(), 'value':b.__unicode__()}]
应该是这样的:
字典= {'ID':b.id,标签:乙.__ UNI code __(),价值:乙.__ UNI code __(),'the_link :baslik /+ b .__ UNI code __()}
这篇关于Django的jQuery的的UI自动完成URL变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!