jquery autocomplete json and clickable link through

后端 未结 2 1540
失恋的感觉
失恋的感觉 2021-02-11 07:09

I\'ve been at this for a while and I\'m making very slow progress mostly because my jquery skills need improvement, I am trying though :)

I have this code:



        
2条回答
  •  渐次进展
    2021-02-11 07:54

    I managed to solve my problem, see below (NOTE: $token is a php variable). This allows me to send (specifically post) more than 1 variable to the php script that returns the JSON response. In my case this is necessary as I use a token to prevent unauthorised access to the search functionality.

    jQuery(function() {
      jQuery("#search").autocomplete({
        source: function(request, response) {
          jQuery.ajax({
            url: "index.php?option=com_eat&view=search&format=raw",
            type: "post",
            dataType: "json",
            data: {
              search_string: request.term,            
              "'.$token.'": "1"
            },
            success: function(data) {
              response(jQuery.map(data, function(item) {
                return {
                    url: item.url,
                    value: item.name
                }
              }))
            }
          })
        },
        select: function( event, ui ) {
          window.location.href = ui.item.url;
        },
        minLength: 2
      });
    });
    

    The returned JSON from index.php?option=com_eat&view=search&format=raw looks like:

    [{"url":"url1", "name":"name1"}, {"url":"url2", "name":"name2"}, ...]
    

    The HTML on the page looks like so:

    
    

提交回复
热议问题