我想检测文本框的内容何时发生了变化。我可以使用keyup方法,但这也将检测不生成字母的击键,如方向键。我想到了两种使用keyup事件的方法:
显式检查所按键的ascii码是否为字母\backspace\delete 使用闭包来记住在击键之前文本框中的文本是什么,并检查这是否已更改。
两者看起来都有点麻烦。
我想检测文本框的内容何时发生了变化。我可以使用keyup方法,但这也将检测不生成字母的击键,如方向键。我想到了两种使用keyup事件的方法:
显式检查所按键的ascii码是否为字母\backspace\delete 使用闭包来记住在击键之前文本框中的文本是什么,并检查这是否已更改。
两者看起来都有点麻烦。
当前回答
这里有一个完整的工作示例。
<html>
<title>jQuery Summing</title>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
$(document).ready(function() {
$('.calc').on('input', function() {
var t1 = document.getElementById('txt1');
var t2 = document.getElementById('txt2');
var tot=0;
if (parseInt(t1.value))
tot += parseInt(t1.value);
if (parseInt(t2.value))
tot += parseInt(t2.value);
document.getElementById('txt3').value = tot;
});
});
</script>
</head>
<body>
<input type='text' class='calc' id='txt1'>
<input type='text' class='calc' id='txt2'>
<input type='text' id='txt3' readonly>
</body>
</html>
其他回答
这个怎么样:
< jQuery 1.7
$("#input").bind("propertychange change keyup paste input", function(){
// do stuff;
});
jQuery 1.7
$("#input").on("propertychange change keyup paste input", function(){
// do stuff;
});
这适用于IE8/IE9, FF, Chrome
“change”事件不能正常工作,但“input”是完美的。
$('#your_textbox').bind('input', function() {
/* This will be fired every time, when textbox's value changes. */
} );
使用textchange事件通过定制的jQuery shim跨浏览器输入兼容性。http://benalpert.com/2013/06/18/a-near-perfect-oninput-shim-for-ie-8-and-9.html(最近分叉github: https://github.com/pandell/jquery-splendid-textchange/blob/master/jquery.splendid.textchange.js)
这处理所有输入标签,包括<textarea>内容</textarea>,这并不总是与改变keyup等工作(!)只有jQuery on("input propertychange")处理<textarea>标签一致,以上是对所有不理解输入事件的浏览器的一个填充。
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/pandell/jquery-splendid-textchange/master/jquery.splendid.textchange.js"></script>
<meta charset=utf-8 />
<title>splendid textchange test</title>
<script> // this is all you have to do. using splendid.textchange.js
$('textarea').on("textchange",function(){
yourFunctionHere($(this).val()); });
</script>
</head>
<body>
<textarea style="height:3em;width:90%"></textarea>
</body>
</html>
JS Bin测试
这还可以处理粘贴、删除,并且不会重复keyup上的工作。
如果不使用shim,请使用jQuery on("input propertychange")事件。
// works with most recent browsers (use this if not using src="...splendid.textchange.js")
$('textarea').on("input propertychange",function(){
yourFunctionHere($(this).val());
});
我想问你为什么要检测文本框的内容在实时改变?
另一种方法是设置一个计时器(通过setIntval?),并将上次保存的值与当前值进行比较,然后重置计时器。这将保证捕捉任何变化,无论是由键、鼠标、其他你没有考虑到的输入设备引起的,甚至是JavaScript从应用程序的不同部分改变值(另一种没有人提到的可能性)。
类似这样的东西应该可以工作,主要是因为focus和focusout最终会得到两个不同的值。我在这里使用数据,因为它在元素中存储值,但不涉及DOM。它也是存储与其元素相连接的值的一种简单方法。您也可以轻松地使用范围更高的变量。
var changed = false;
$('textbox').on('focus', function(e) {
$(this).data('current-val', $(this).text();
});
$('textbox').on('focusout', function(e) {
if ($(this).data('current-val') != $(this).text())
changed = true;
}
console.log('Changed Result', changed);
}