javascript Jquery:.on('input') 当输入值被另一个函数改变时不会触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15389169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery: .on('input') not firing when input value changed by another function
提问by preahkumpii
Here's the code:
这是代码:
$("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.
简短的版本是,当我输入此文本输入时,会触发 top 函数。在第二个函数中,我使键盘出现,并根据用户点击更改输入值。但是,当通过第二个函数更改输入值时,第一个函数不会触发。有任何想法吗?非常感激。
回答by Brad M
$("input[type='text']").trigger('input');
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.
把它放在你的第二个函数中。仅使用 javascript 设置文本框的值不会触发输入事件,因此您的输入事件处理程序不会触发。在这种情况下,您需要手动触发该事件处理程序。
Note: You should notbe 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.
注意:您不应该使用输入事件处理程序。查看此链接并查看糟糕的浏览器兼容性。https://developer.mozilla.org/en-US/docs/DOM/window.oninput。更改事件可能会更好地为您服务。