<input type="text">的值可以通过多种方式改变,包括:
按键 复制/粘贴 JavaScript修改 由浏览器或工具栏自动完成
我希望在JavaScript函数发生变化时调用它(使用当前输入值)。我希望它能马上被调用,而不是在输入失去焦点的时候。
我正在寻找最干净和最健壮的方法来跨所有浏览器(最好使用jQuery)。
<input type="text">的值可以通过多种方式改变,包括:
按键 复制/粘贴 JavaScript修改 由浏览器或工具栏自动完成
我希望在JavaScript函数发生变化时调用它(使用当前输入值)。我希望它能马上被调用,而不是在输入失去焦点的时候。
我正在寻找最干净和最健壮的方法来跨所有浏览器(最好使用jQuery)。
当前回答
不幸的是,没有事件或事件集符合您的标准。按键和复制/粘贴都可以用keyup事件处理。通过JS进行更改比较棘手。如果你可以控制设置文本框的代码,你最好的选择是修改它,直接调用你的函数,或者在文本框上触发一个用户事件:
// Compare the textbox's current and last value. Report a change to the console.
function watchTextbox() {
var txtInput = $('#txtInput');
var lastValue = txtInput.data('lastValue');
var currentValue = txtInput.val();
if (lastValue != currentValue) {
console.log('Value changed from ' + lastValue + ' to ' + currentValue);
txtInput.data('lastValue', currentValue);
}
}
// Record the initial value of the textbox.
$('#txtInput').data('lastValue', $('#txtInput').val());
// Bind to the keypress and user-defined set event.
$('#txtInput').bind('keypress set', null, watchTextbox);
// Example of JS code triggering the user event
$('#btnSetText').click(function (ev) {
$('#txtInput').val('abc def').trigger('set');
});
如果你不能控制这段代码,你可以使用setInterval()来“监视”文本框的变化:
// Check the textbox every 100 milliseconds. This seems to be pretty responsive.
setInterval(watchTextbox, 100);
这种主动监控不会“立即”捕捉到更新,但它似乎足够快,没有可察觉的滞后。正如DrLouie在评论中指出的,如果你需要观察大量的输入,这个解决方案可能无法很好地扩展。您总是可以将第二个参数调整为setInterval()以更频繁或更少地检查。
其他回答
如果你不喜欢其他答案,这里有一个稍微不同的解决方案:
var field_selectors = ["#a", "#b"];
setInterval(function() {
$.each(field_selectors, function() {
var input = $(this);
var old = input.attr("data-old-value");
var current = input.val();
if (old !== current) {
if (typeof old != 'undefined') {
... your code ...
}
input.attr("data-old-value", current);
}
}
}, 500);
考虑到您不能依赖于单击和键up来捕获上下文菜单粘贴。
您可以将'input'事件绑定到<input type="text">。这将在每次输入更改(如复制、粘贴、按键等)时触发。
$("#input-id").on("input", function(){
// Your action
})
我已经创建了一个样本。希望它对你有用。
var typingTimer;
var doneTypingInterval = 10;
var finaldoneTypingInterval = 500;
var oldData = $("p.content").html();
$('#tyingBox').keydown(function () {
clearTimeout(typingTimer);
if ($('#tyingBox').val) {
typingTimer = setTimeout(function () {
$("p.content").html('Typing...');
}, doneTypingInterval);
}
});
$('#tyingBox').keyup(function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(function () {
$("p.content").html(oldData);
}, finaldoneTypingInterval);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<textarea id="tyingBox" tabindex="1" placeholder="Enter Message"></textarea>
<p class="content">Text will be replace here and after Stop typing it will get back</p>
http://jsfiddle.net/utbh575s/
这段jQuery代码使用.bind()来捕获对任何元素的即时更改,并且应该在所有浏览器上工作:
$('.myElements').each(function() {
var elem = $(this);
// Save current value of element
elem.data('oldVal', elem.val());
// Look for changes in the value
elem.bind("propertychange change click keyup input paste", function(event){
// If value has changed...
if (elem.data('oldVal') != elem.val()) {
// Updated stored value
elem.data('oldVal', elem.val());
// Do action
....
}
});
});
但是,请注意.bind()在jQuery 3.0版本中已弃用。任何使用jQuery 1.7或更新版本的人都应该使用.on()。
你就不能用<span contenteditable="true" spellcheck="false">元素来代替<input type="text">吗?
<span>(以contenteditable="true" spellcheck="false"作为属性)与<input>区别很大,主要是因为:
它的样式不是<input>。 它没有value属性,但文本被呈现为innerText,并成为其内部主体的一部分。 它是multiline,而<input>不是,尽管你设置了属性multiline="true"。
要完成外观,当然,你可以用CSS样式,而将值写成innerText,你可以为它获得一个事件:
这是小提琴。
不幸的是,有一些东西在IE和Edge中实际上不起作用,我无法找到。