简体   繁体   中英

Jquery UI Autocomplete with JSON not working

Hey guys I am trying to figure out why the autocomplete() method is not doing a GET when I add a variable to the end of the source. For example:

    <script> 
    $(document).ready(function(){
        var search_input;

        $('#search').keyup(function(){
            search_input = ($(this).val());
            console.log(search_input);
        });
        $('#search').autocomplete({
          source: "http://192.168.33.10/app_dev.php/search/query/" + search_input,
          minLength: 2
        });
    });
    </script>

    <div class="ui-widget">
        <label for="search">Search</label>
        <input type="text" id="search" />
    </div>   

Yet if I remove + search_input from the source it will do a GET , like so..

    <script> 
    $(document).ready(function(){
        var search_input;

        $('#search').keyup(function(){
            search_input = ($(this).val());
            console.log(search_input);
        });
        $('#search').autocomplete({
          source: "http://192.168.33.10/app_dev.php/search/query/",
          minLength: 2
        });
    });
    </script>

    <div class="ui-widget">
        <label for="search">Search</label>
        <input type="text" id="search" />
    </div>   

Even though you're changing search_input , the widget is only getting the URL with the initial value of search_input (which is probably empty).

Something like this is what you need:

$('#search').autocomplete({
    source: function (request, response) {
        $.get("http://192.168.33.10/app_dev.php/search/query/" + request.term, response)
    },
    minLength: 2
});

In your code, the autocomplete function just needs to provide the source parameter with a callback function. source: function( request, response ) {}

You can follow the example for Multiple remote suggestions on jQuery UI Autocomplete

    <script type="text/javascript"> 
      $(function() {
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }

    $( "#search" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).data( "ui-autocomplete" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        source: function( request, response ) {
          $.getJSON( "http://192.168.33.10/app_dev.php/search/query/" + extractLast( request.term ), {
            term: extractLast( request.term )
          }, response );
        },
        search: function() {
          // custom minLength
          var term = extractLast( this.value );
          if ( term.length < 2 ) {
            return false;
          }
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( "" );
          this.value = terms.join( ", " );
          return false;
        }
      });
  });
        </script>

The JSON returned by the server in this example would be structured like this:

[{"id":"1544","label":"Suggestion 1","value":"Suggestion 1"},
{"id":"3321","label":"Suggestion 2","value":"Suggestion 2"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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