jQuery改变不起作用的动态价值变化 [英] jquery change not working incase of dynamic value change

查看:93
本文介绍了jQuery改变不起作用的动态价值变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文本框的值从某个其他事件动态变化,那么在jQuery中如何跟踪 onchange 事件。我试过这个,但是它不工作:

  $(#txtbox)。change(function(){
alert(change);
});


解决方案

keyup 不涵盖所有自然情况:(



两种情况,当更改 keyup 将无法正常工作:




  • 浏览器窗体自动完成机制

  • 系统剪贴板中的鼠标执行粘贴。



示例:form有要将电子邮件地址放入其中的文本框,而onchange,keyup



当用户开始输入电子邮件地址时,如果Chrome或Firefox自动填充框弹出,用户点击之一选项,框中填满了值,焦点保留在文本框内(所以没有更改事件触发),没有按键被按下(没有keyup事件),我尝试附加点击列表,但它导致我的浏览器中的奇怪的行为



与粘贴类似 - 如果鼠标用于粘贴(通过右键单击可用的上下文菜单)),ke任何人都有一个完整的解决方案,即如何处理输入值的实际变化?/ p>


In jQuery how can I track onchange event if the value of textbox is changing dynamically from some other event. I tried this but its not working:

$("#txtbox").change(function(){
   alert("change");
});

解决方案

keyup doesn't cover all natural cases :(

Two cases when change and keyup won't work:

  • browser form autocomplete mechanisms
  • mouse-performed paste from system clipboard.

Example: form has textbox intended to put an email address into it, and onchange, keyup events are attached to it.

When user starts typing an email address, if e.g. Chrome or Firefox autocomplete box pops up and user clicks one of the options, the box gets filled with the value, focus stays within the textbox (so no change event firing) and no key was pressed (no keyup event). I tried attaching click to the list, but it causes weird behavior in my browser.

Similar with pasting - if mouse is used for pasting (through contextual menu available on right mouse click)), neither keyup nor change events are fired.

Anyone has a complete solution for this, i.e. how to handle actual changes of the value of the input?

这篇关于jQuery改变不起作用的动态价值变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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