Run function right after the input changes [duplic

2019-08-11 00:50发布

问题:

Possible Duplicate:
bootstrap-typeahead.js add a listener on select event

I'm using Twitter Bootstrap's typeahead functionality, and I want to use .on('change', function(){...}), but my problem is, the event fires before the value of the input actually changes to the one selected, and when the function is called, the $(this).val() inside the function returns the value that was there before the change. The event fires twice, but only the second one should.

I need a way to run the function in a way so it returns the selected value, and runs only once.

jsFiddle

回答1:

Use the updater property:

http://jsfiddle.net/ZUhFy/10/

.typeahead({
        source: namelist,
        updater: function(item) {
            $('#log').append(item + '<br>');
            return item;
        }
    });

updater returns selected item The method used to return selected item. Accepts a single argument, the item and has the scope of the typeahead instance. http://twitter.github.com/bootstrap/javascript.html#typeahead



回答2:

try

$('#findname').on('keyup', function() {...});


回答3:

Try using the keyup

.on('keyup', function(){...})