Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- $block = $('.header-center__search');
- function autocomplete() {
- var $this = $('#search'),
- $url = $('.header-center__search').data('url'),
- query = $this.val(),
- data = {
- 'query': query
- };
- // вспрывающее окно с подсказками
- if(query.length >= 3) {
- $('.autocomplet').remove();
- $block.append('<ul class="autocomplet"></ul>');
- $.post($url, data).done(function (data) {
- var resalts = data['response'],
- ids = data['ids'];
- for(var i = 0; i < resalts.length; i++){
- $('.autocomplet').append(
- '<div class="autocomplet__div" data-url="/farm/drug/'+ ids[i] +'/"><li class="autocomplet__li">'+ resalts[i] +'</li></div>'
- );
- }
- })
- }
- if(query.length < 3){
- $('.autocomplet').remove();
- }
- }
- $('#search').on('keyup', function(event){
- setTimeout(autocomplete, 3000);
- });
- //выбор подсказки по клику мышки
- $($block).on('click', '.autocomplet__div', function (){
- var $url = $(this).data('url');
- window.location = $url;
- });
- $(document).mouseup(function (e) {
- var container = $('.autocomplet');
- if (container.has(e.target).length === 0){
- container.hide();
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement