可以默认“Term"吗?在“jquery UI 自动完成"中传递的名称;功能要改? [英] Can the default "Term" name passed in the "jquery UI autocomplete" feature be changed?

查看:12
本文介绍了可以默认“Term"吗?在“jquery UI 自动完成"中传递的名称;功能要改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jquery ui 自动完成功能更改默认设置的术语"字段.是否可以轻松地将其更改为q"(查询)而无需在核心"文件中进行更改?

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:

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> 

推荐答案

是的,可以通过发出自己的 AJAX 请求来实现.

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

假设您有以下设置:

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

默认情况下自动完成(如您所见)发送如下所示的请求:

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

myservice.xyz?term=abc"

myservice.xyz?term=abc"

您可以提供对自动完成的 source 选项的函数引用.在该函数中,您可以发出自己的 AJAX 请求,如下所示:

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

myservice.xyz?q=abc

这篇关于可以默认“Term"吗?在“jquery UI 自动完成"中传递的名称;功能要改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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