Run function right after the input changes [duplic

2019-08-11 00:31发布

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

3条回答
成全新的幸福
2楼-- · 2019-08-11 00:52

try

$('#findname').on('keyup', function() {...});
查看更多
劫难
3楼-- · 2019-08-11 00:59

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

查看更多
贼婆χ
4楼-- · 2019-08-11 01:08

Try using the keyup

.on('keyup', function(){...})
查看更多
登录 后发表回答