Can the default “Term” name passed in the “jquery UI autocomplete” feature be changed?

后端 未结 2 969
逝去的感伤
逝去的感伤 2020-12-30 15:22

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

相关标签:
2条回答
  • 2020-12-30 15:59

    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

    0 讨论(0)
  • 2020-12-30 16:05

    You could use the callback source option and make your own request.

    http://jqueryui.com/demos/autocomplete/

    0 讨论(0)
提交回复
热议问题