输入更改后立即运行功能 [英] Run function right after the input changes
问题描述
我正在使用Twitter Bootstrap的预先输入功能,我想使用 .on('change',function(){...})
,但我的问题是,事件在输入值实际变为选定值之前触发,并且在调用函数时, $(this).val()
函数内部返回更改前的值。事件会触发两次,但只有第二次触发。
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.
推荐答案
使用updater属性:
Use the updater property:
.typeahead({
source: namelist,
updater: function(item) {
$('#log').append(item + '<br>');
return item;
}
});
updater返回所选项目用于返回所选项目的方法。
接受单个参数,该项目并具有typeahead
实例的范围。 http://twitter.github.com/bootstrap/javascript.html#typeahead
这篇关于输入更改后立即运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!