开发者

jquery autocomplete enter key

开发者 https://www.devze.com 2023-01-20 14:32 出处:网络
Currently the autocomplete action when pressing the Enter key on a selected element is to put that elements value into the input box.

Currently the autocomplete action when pressing the Enter key on a selected element is to put that elements value into the input box.

How can I modify the b开发者_如何学编程ehavior so that pressing the Enter key on an element triggers that elements embedded url. To simplify I'd like to make the Enter key have the same behavior as a mouse click on a returned element.

Thanks


Easy !

$(".quicksearch-input").autocomplete({

  minLength: 4, // minimum length to trigger suggestions

  // ... + other configuration options

  select: function(e, ui) { // define select handler

    var uri = ui.item.link; // where ui.item.link itself is defined in your object which you composing when retrieving JSON from endpoint

    window.location = uri; // forwarding to the URL obtained

  }, // select //
});

But everything depends on your way of implementation.


This is the function that is called when you select an item and hit enter. The hacky code you see in this function extracts the href from the link and redirects to that url.

$("#searchBox").result(function(event, data, formatted) {
        var p = data.toString();
        p = p.replace('<a href="', '');
        var url = p.substr(0, p.indexOf('"'));
        p = p.replace('</a>', '');
        p = p.substr(p.indexOf('>') + 1, p.length - p.indexOf('>') - 1);
        window.location = "" + url;
        return false;
    });


Did you try keypress event

have a look on the events

http://api.jquery.com/keypress/

http://api.jquery.com/category/events/keyboard-events/

http://api.jquery.com/category/events/mouse-events/

0

精彩评论

暂无评论...
验证码 换一张
取 消