javascript - Bootstrap Typeahead click event - Stack Overflow

I'm using bootstrap-typeahead.js v2.0.0 for an autoplete search. I have a click event to view the

I'm using bootstrap-typeahead.js v2.0.0 for an autoplete search. I have a click event to view the result but it only works like 1 of 10 times, in the other case I get an error: "Uncaught SyntaxError: Unexpected token u".

I've looked around and found this: and I tried the solutions there but nothing seems to work. It works perfect when I use the enter-key so it has to be something concerning the click-event. Anyone else got the same problem? Code:

$('#search').typeahead({

        source: function (typeahead, query) {
                $.ajax({
                    type: "GET",
                    url: "search/" + query,
                    success: function (data) {
                        searchResult = data;
                        return typeahead.process(data);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log(thrownError);
                    }
                }); 
        }
        },
        onselect: function (obj) {
            window.location.href = "view/" + obj.id;
        },
        items: 8,
        minLength: 1,
        highlighter: function (item) {
        var artist = _.find(searchResult, function (a) {
            return item === a.value;
        });
        return ('<li>' + artist.value + ' (' + artist.dbirth + '-' + artist.ddeath + ')<li>');
        }
    });

I'm using bootstrap-typeahead.js v2.0.0 for an autoplete search. I have a click event to view the result but it only works like 1 of 10 times, in the other case I get an error: "Uncaught SyntaxError: Unexpected token u".

I've looked around and found this: https://github./twitter/bootstrap/issues/4018 and I tried the solutions there but nothing seems to work. It works perfect when I use the enter-key so it has to be something concerning the click-event. Anyone else got the same problem? Code:

$('#search').typeahead({

        source: function (typeahead, query) {
                $.ajax({
                    type: "GET",
                    url: "search/" + query,
                    success: function (data) {
                        searchResult = data;
                        return typeahead.process(data);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log(thrownError);
                    }
                }); 
        }
        },
        onselect: function (obj) {
            window.location.href = "view/" + obj.id;
        },
        items: 8,
        minLength: 1,
        highlighter: function (item) {
        var artist = _.find(searchResult, function (a) {
            return item === a.value;
        });
        return ('<li>' + artist.value + ' (' + artist.dbirth + '-' + artist.ddeath + ')<li>');
        }
    });
Share Improve this question asked Mar 31, 2013 at 23:06 bangerangbangerang 4831 gold badge7 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Okay I solved it myself. This is what you have to do:

Open bootstrap-typeahead.js and find the listen method on row 203 and modify like this:

    listen: function () {
    this.$element
    .on('blur',     $.proxy(this.blur, this))
    .on('keypress', $.proxy(this.keypress, this))
    .on('keyup',    $.proxy(this.keyup, this))

    // if ($.browser.webkit || $.browser.msie) {
    this.$element.on('keydown', $.proxy(this.keypress, this))
    // }

    this.$menu
    .on('click', $.proxy(this.click, this))
    .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
    .on('mouseleave', 'li', $.proxy(this.mouseleave, this))
    }

The only difference here is that I added a listener on 'mouseleave'.

Go to the mouseenter method on row 278 and change it to this:

mouseenter: function (e) {
      $(e.currentTarget).addClass('active')
    }

Then add a new method named 'mouseleave' and add this code:

mouseleave: function () {
      this.$menu.find('.active').removeClass('active')
}

Hopefully this will help anyone with a similar problem.

Here is a much simpler solution. The "Blur" event binds before the click event. Simply add a delay for the blur and this will fix the problem.

<input type="text" placeholder="Enter a Name" onblur="hidesuggest();" id="suggestbox">
<script>
function hidesuggest(){
    setTimeout("$('#suggestbox').toggle();",200);
}
</script>

The extra 200 milliseconds gives enough time for the "click" binding to execute on the mouse click.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745542038a4632169.html

相关推荐

  • javascript - Bootstrap Typeahead click event - Stack Overflow

    I'm using bootstrap-typeahead.js v2.0.0 for an autoplete search. I have a click event to view the

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信