[英]How can I check the new value of a text field on keypress?
I have a single form field and I need to be able to detect when the field changes and execute some additional code using the new value of the field. 我有一个表单字段,我需要能够检测字段何时更改并使用字段的新值执行一些其他代码。 I only want to execute the code if the key pushed actually changes the value of the text box.
如果按下的键实际上改变了文本框的值,我只想执行代码。 Here's the solution I came up with.
这是我提出的解决方案。
function onkeypress(e) {
var value = this.value;
// do something with
}
function onkeyup(e) {
if ( e.which == 8 || e.keyCode == 46 ) { // delete or backspace keys
this.onkeypress(e);
}
}
There's just one problem though. 但是只有一个问题。 onkeypress is fired before the field's value is updated, so when I grab the value, I'm getting the previous value.
onkeypress在字段值更新之前被触发,所以当我获取值时,我得到之前的值。 I would use keyup exclusively if I knew a way to test whether the key changed the value or not, but some characters (like the arrow keys) have no effect on the field's value.
如果我知道一种方法来测试密钥是否改变了值,我会专门使用密钥,但是某些字符(如箭头键)对字段的值没有影响。
Any ideas? 有任何想法吗?
这很简单,只需使用onkeyup而不是onkeypress
The way I do this is simply with a variable along the lines of prevValue
. 我这样做的方法只是使用
prevValue
的变量。 At the end of the key press function, store the value in that variable, and only execute the function again if the value doesn't equal that previous value. 在按键功能结束时,将值存储在该变量中,如果该值不等于该值,则仅再次执行该功能。
have another conditional: 有另一个条件:
if(e.keyCode > 31 && e.keyCode < 127) {
value = this.value + e;
}
This would catch any low level ascii symbol entered by the keyboard outside of the special char range. 这将捕获键盘在特殊字符范围之外输入的任何低级ascii符号。
EDIT: (32 is space) 编辑:(32是空格)
function onkeyup(e) {
if ( e.which == 8 || e.keyCode == 46 ) { // delete or backspace keys
switch(e.keyCode) {
case 32:
case 48..90:
case 96..111:
case 188:
case 190..192:
case 219..222:
value = this.value + e;
break;
default:
break;
}
this.onkeypress(e);
}
}
Here's my final solution, which uses a prevValue variable but doesn't pollute the global namespace (window) or the dom element's properties. 这是我的最终解决方案,它使用prevValue变量,但不会污染全局命名空间(窗口)或dom元素的属性。
(function() {
var input = document.getElementById('input_box'),
prevValue;
input.onkeyup = function(e) {
if ( this.value != prevValue ) {
prevValue = this.value;
// execute some more code here...
}
}
}());
Notice above that the onkeyup function serves as a closure around the prevValue variable. 请注意,onkeyup函数用作prevValue变量的闭包。 This prevents the namespace pollution so I don't have to create a global variable or attach a property to the input element itself.
这可以防止命名空间污染,因此我不必创建全局变量或将属性附加到input元素本身。 This is as elegant as I can get it.
这是我能得到的优雅。 I actually wrote mine in jQuery but thought the answer itself should be pure JavaScript...
我实际上是用jQuery编写的,但认为答案本身应该是纯JavaScript ...
It's kind of a hack, but you can put the previous value in a property of the textbox (called "expando attributes") 这是一种黑客行为,但您可以将之前的值放在文本框的属性中(称为“expando attributes”)
function onkeypress(e) {
this.oldvalue = this.value;
}
function onkeyup(e) {
if (this.value != this.oldvalue) {
// do something
}
}
You could store the old value and detect if it changed: 您可以存储旧值并检测它是否已更改:
function keyUpHandle(e) {
if (this.prevValue && this.prevValue != this.value) {
// do something
}
this.prevValue = this.value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.