输入更改后立即运行功能 [英] Run function right after the input changes

查看:93
本文介绍了输入更改后立即运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

bootstrap-typeahead.js在select事件上添加一个监听器

我正在使用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.

jsFiddle

推荐答案

使用updater属性:

Use the updater property:

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

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




updater返回所选项目用于返回所选项目的方法。
接受单个参数,该项目并具有typeahead
实例的范围。 http://twitter.github.com/bootstrap/javascript.html#typeahead

这篇关于输入更改后立即运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆