Jquery ui autocomplete: start only when the item is not selected

I am trying to figure out how to make the "opposite" of jquery ui select. When someone does not select a parameter and simply displays "jquery something", the output will be: msgstr "new entry: jquery something". However, when "jquery" is selected, it would be nice to somehow only "select from the list: jquery" and prevent the keystroke from being flashed. However, both events shoot. I am trying to do this one or the other.

<input class="test" type="text" />

<script>
$('.test').autocomplete({
    select: function (event, ui) {
        alert('selected from list: ' + ui.item.label);
        event.preventDefault();
        return false;
    },
    source: ["jquery", "jquery ui", "sizzle"]
});
$('.test').live('keypress', function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        alert('new input: ' + $(this).val());
    }
});
</script>

This is based on the assumption that the enter key is used to select an option from the ui menu.

+5
source share
1 answer

, autocompletechange. , , -, :

$("#auto").autocomplete({
    source: ['hi', 'bye', 'foo', 'bar'],
    change: function(event, ui) {
        console.log(this.value);
        if (ui.item == null) {
            $("span").text("new item: " + this.value);
        } else {
            $("span").text("selected item: " + ui.item.value);
        }
    }
});

: http://jsfiddle.net/Ne9FH/

+8

All Articles