简体   繁体   English

Django jquery-ui自动完成URL更改

[英]Django jquery-ui autocomplete url changing

I'm using the jquery-ui autocomplete in an input. 我在输入中使用了jquery-ui自动完成功能。 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. 它适合自动完成,但是当我选择输入的完整版本并按Enter时,它会转到/?n=something链接,我想将此链接更改为/baslik/something ,应将其更改为/baslik/something 。每个输入。 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>

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. 上面代码的最后js部分是当用户点击输入以进入/baslik/something时转换输入,但是如果我不从自动完成更改输入就可以做到这一点。

The view: 风景:

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))

the url: 网址:

url(r'^autoco/$', autoco, name='autoco'),

How can I make this work in my way? 如何以我的方式进行这项工作? Thanks. 谢谢。

There is a solution. 有一个解决方案。 This is js part of jquery-ui autocomplete: 这是jquery-ui自动完成的js部分:

<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>

We added the_link attribute to it and we should describe it in our view: this part: 我们向其中添加了the_link属性,我们应该在我们的视图中对其进行描述:这一部分:

dict = {'id':b.id, 'label':b.__unicode__(), 'value':b.__unicode__()}]

should be like this: 应该是这样的:

dict = {'id':b.id, 'label':b.__unicode__(), 'value':b.__unicode__(), 'the_link':"baslik/"+b.__unicode__()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM