Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQueryUI Autocomplete with clickable results

I've tried looking for similar cases and I found some but each time, the code is slightly different and I don't manage finding the solution...

I'm using jQuery Autocomplete on my website whith datas from a mysql database.
The results are sorted by categories to display both products and brands in the same input
So I naturaly pasted the example from their website on mine and it works fine!
The generated json look like this

{"label":"Product 1","url":"product.php?id=1","category":"Products"}

My only problem is that I would like results to be clickable. So when the user clicks on a result, an other page loads instead of the default behaviour which is filling the input with the data.

I have created a demo on jsfiddle so you can see what's happening
http://jsfiddle.net/fJ22W (datas are contained in the js here)

Your help is more than welcome, I guess this is not such a big deal but my poor skills in jQuery prevents me to resolve that problem...

Bertrand

like image 809
Bertrand Avatar asked Sep 06 '12 12:09

Bertrand


People also ask

How can create Autocomplete search box in jQuery?

For this, we are going to use the jQuery inbuilt function called autocomplete. We will do that in two different stages. Create a basic HTML file and add an input bar to it. Implement the autocomplete functionality.

How does jQuery Autocomplete work?

In the process of searching a specific value, the jQuery UI autocomplete selection feature provides the user with some string suggestions for the input area which effectively saves time. The autocomplete select action is triggered when the user selects one of the options from the pre-populated list.

What is UI Autocomplete?

Description: Autocomplete enables users to quickly find and select from a pre-populated list of values as they type, leveraging searching and filtering.

How display image in jQuery ui Autocomplete textbox in PHP?

Here we will make autocomplete textbox, in which make list pre-populated search result with image. This feature we will make by add custom html tag in jQuery UI autocomplete method by add _renderItem. Here we will use __renderItem method, by using this method we will add custom HTML code in autocomplete textbox.


1 Answers

Use the select event:

$( "#search" ).catcomplete({
        source: 'suggest_zip.php',
        select: function( event, ui ) {
            window.location = ui.item.url;
        }
    });

Obviously you'll want to have some validation around the url etc.

For what it's worth I'd also recommend using the default autocomplete widget and using the events and options rather than trying to inherit from it. Your code will be much cleaner and less chance of odd bugs.

like image 157
Chao Avatar answered Nov 06 '22 02:11

Chao