代码如下:
$("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() );
});
});
简短的版本是,当我输入此文本输入时,顶部函数会触发。在第二个函数中,我显示一个键盘,并根据用户点击更改输入的值。但是,当通过第二个函数更改输入值时,第一个函数不会触发。有任何想法吗?非常感谢。
最佳答案
$("input[type='text']").trigger('input');
将其放入第二个函数中。 仅使用 javascript 设置文本框的值不会触发输入事件,因此您的输入事件处理程序不会触发。在这种情况下,您需要手动触发该事件处理程序。
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.
关于javascript - Jquery: .on ('input' ) 当输入值被另一个函数更改时不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15389169/