可以将默认"术语;名字传入的" jQuery用户界面自动完成"功能被改变? [英] Can the default "Term" name passed in the "jquery UI autocomplete" feature be changed?

查看:109
本文介绍了可以将默认"术语;名字传入的" jQuery用户界面自动完成"功能被改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改默认情况下使用jQuery UI的自动完成功能设置为期限字段中。它是有可能很容易地将其更改为Q(查询)足不出户,并在核心文件改变了吗?

JavaScript的:

 <脚本>
    $(函数(){
        $(#spotify_song_search).autocomplete({
            来源:http://ws.spotify.com/search/1/track.json
            数据:{
                问:request.term
            },
            数据类型:的getJSON
            的minLength:3,
            选择:函数(事件,UI){
                警报('选择');
            }
        });
    });
< / SCRIPT>


解决方案

是的,它通过使自己的Ajax请求是可能的。

假设你有以下设置:

  $(#MyField的)。自动完成({
    来源:/my_url/myservice.xyz
});

在默认情况下自动完成(如你注意到)发送看起来像请求:


  

myservice.xyz?term=abc


您可以提供一个功能参照自动完成的选项。内部的功能,您可以制作自己的AJAX请求,这将是这样的:

  $(#MyField的)。自动完成({
     来源:函数(请求,响应){
         // request.term是搜索的任期。
         //响应回调函数必须调用更新自动完成的
         //建议名单。
         $阿贾克斯({
             网址:/my_url/myservice.xyz
             数据:{Q:request.term},
             数据类型:JSON
             成功:响应,
             错误:函数(){
                 响应([]);
             }
         });
     });
});

这应该产生一个请求看起来更像是:


  

myservice.xyz?q=abc


I am trying to change the "term" field that is set to that by default with the jquery ui autocomplete feature. Is it possibly to easily change it to "q" (query) without going and changing it in the "core" file?

JavaScript:

<script>
    $(function() {
        $( "#spotify_song_search" ).autocomplete({
            source: "http://ws.spotify.com/search/1/track.json",
            data: { 
                q: request.term 
            },
            dataType: "getjson",
            minLength: 3,
            select: function( event, ui ) { 
                alert('select'); 
            }
        });
    });
</script> 

解决方案

Yes, it's possible by making your own AJAX request.

Assume you have the following setup:

$("#myfield").autocomplete({
    source: '/my_url/myservice.xyz'
});

Autocomplete by default (as you noticed) sends requests that look like:

myservice.xyz?term=abc"

You can supply a function reference to the source option of autocomplete. Inside that function you can make your own AJAX request, which would look like this:

$("#myfield").autocomplete({
     source: function (request, response) {
         // request.term is the term searched for.
         // response is the callback function you must call to update the autocomplete's 
         // suggestion list.
         $.ajax({
             url: "/my_url/myservice.xyz",
             data: { q: request.term },
             dataType: "json",
             success: response,
             error: function () {
                 response([]);
             }
         });
     });
});

This should generate a request looking more like:

myservice.xyz?q=abc

这篇关于可以将默认&QUOT;术语;名字传入的&QUOT; jQuery用户界面自动完成&QUOT;功能被改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆