Jquery:.on('input')当输入值被另一个函数改变时不触发 [英] Jquery: .on('input') not firing when input value changed by another function

查看:74
本文介绍了Jquery:.on('input')当输入值被另一个函数改变时不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:

$("input[type='text']").on('input', function() { ...stuff here... });  //first function

$('input[type="text"]').click(function() {  //second function
  $('#keyboard').show();
  $('#keyboard .letter').click(function() {
    var currentValue = $('input[type="text"]').val();
    alert(currentValue);
    $('input[type="text"]').val( currentValue + $(this).text() );
  });
});

简短版本是,当我输入此文本输入时,顶部函数将触发。在第二个功能中,我会出现一个键盘,并根据用户点击更改输入的值。但是,当通过第二个函数改变输入值时,第一个函数不会触发。有任何想法吗?非常感谢。

The short version is, when I type in this text input, the top function fires. In the second function, I make a keyboard appear, and change the value of the input based on user clicks. However, the first function is not firing when the input value is changed by means of the second function. Any ideas? Much appreciated.

推荐答案

$("input[type='text']").trigger('input');

将它放在你的第二个函数中。
仅使用javascript设置文本框的值不会触发输入事件,因此您的输入事件处理程序不会触发。在这种情况下,您需要手动触发该事件处理程序。

Put that in your second function. Merely setting the value of a textbox using javascript will not trigger the input event, thus your input event handler will not fire. In this scenario, you need to trigger that event handler manually.


注意:您应该使用输入事件处理程序请参阅此链接,查看可怕的浏览器兼容性。 https://developer.mozilla.org/en-US/docs/ DOM / window.oninput 。相反,你可能会更好地为改变事件服务。

Note: You should not be using the input event handler. See this link and look at the the awful browser compatibility. https://developer.mozilla.org/en-US/docs/DOM/window.oninput . You will probably be better served by the change event instead.

这篇关于Jquery:.on('input')当输入值被另一个函数改变时不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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