将文本更改侦听器添加到文本框 [英] Adding text change listener to a text box

查看:55
本文介绍了将文本更改侦听器添加到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本更改侦听器添加到文本框.但是,不仅可以通过输入文本来手动更改文本.为此,我可以只使用keyup侦听器.实际上,文本字段用于日期,而我有一个日历,可用于选择日期并填充文本字段.现在,只要文本字段发生更改,我都想触发一个动作.那么我应该使用哪种监听器?

I am trying to add a text change listener to a text box. However, the text is not only changed manually by entering the text. For that I can just use the keyup listener. Actually, the text field is for the date and I have a calendar which can be used to select the date and populate the text field. Now whenever there is a change in the text field I want to trigger an action. So which kind of listener should I use?

推荐答案

根据需要支持的浏览器,您可以使用HTML5的输入.当监视的输入发生更改时,它将立即触发.在jQuery中,您只需要这样做:

Depending on the Browsers you need to support you might use HTML5's oninput. It fires immediately when the monitored input changes. In jQuery you would just do:

$('#my-input').bind('input', function(e) {
    console.log("#my-input's value changed");
});

更新:不幸的是,此方法将不起作用,因为事件不会在输入值的脚本更改上触发.您将不得不像"OverZealous"中提到的那样手动"挂上日历.

Update: Unfortunately this method won't work as the event doesn't fire on scripted changes of the input's value. You will have to hook into your calender "manually" just like OverZealous mentioned.

这篇关于将文本更改侦听器添加到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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