如何在按键时检查文本字段的新值?

发布于 2024-08-18 19:31:34 字数 472 浏览 9 评论 0原文

我有一个表单字段,我需要能够检测该字段何时发生更改,并使用该字段的 new 值执行一些附加代码。我只想在按下的键实际上更改了文本框的值时执行代码。这是我想出的解决方案。

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);
  }
}

但只有一个问题。 onkeypress 在字段值更新之前触发,因此当我获取该值时,我将获取之前的值。如果我知道一种方法来测试键是否更改值,我会专门使用 keyup,但某些字符(如箭头键)对字段的值没有影响。

有什么想法吗?

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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

可遇━不可求 2024-08-25 19:31:34

这很简单,只需使用 onkeyup 而不是 onkeypress

this is easy, just use onkeyup instead of onkeypress

一人独醉 2024-08-25 19:31:34

我执行此操作的方法只是使用一个类似于 prevValue 的变量。在按键函数结束时,将值存储在该变量中,并且仅当该值不等于先前的值时才再次执行该函数。

The way I do this is simply with a variable along the lines of 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.

何处潇湘 2024-08-25 19:31:34

还有另一个条件:

if(e.keyCode > 31  && e.keyCode < 127) {
    value = this.value + e;
}

这将捕获特殊字符范围之外键盘输入的任何低级 ascii 符号。

编辑:(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);        
  }        
}

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.

EDIT: (32 is space)

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);        
  }        
}
浮云落日 2024-08-25 19:31:34

这是我的最终解决方案,它使用 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...
    }
  }
}());

请注意,上面的 onkeyup 函数充当 prevValue 变量周围的闭包。这可以防止命名空间污染,因此我不必创建全局变量或将属性附加到输入元素本身。这是我所能得到的最优雅的。我实际上是用 jQuery 编写的,但认为答案本身应该是纯 JavaScript...

Here's my final solution, which uses a prevValue variable but doesn't pollute the global namespace (window) or the dom element's properties.

(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. This prevents the namespace pollution so I don't have to create a global variable or attach a property to the input element itself. This is as elegant as I can get it. I actually wrote mine in jQuery but thought the answer itself should be pure JavaScript...

‖放下 2024-08-25 19:31:34

这是一种黑客攻击,但您可以将以前的值放入文本框的属性中(称为“expando 属性”)

function onkeypress(e) {
  this.oldvalue = this.value;
}

function onkeyup(e) {
  if (this.value != this.oldvalue) {
    // do something
  }
}

It's kind of a hack, but you can put the previous value in a property of the textbox (called "expando attributes")

function onkeypress(e) {
  this.oldvalue = this.value;
}

function onkeyup(e) {
  if (this.value != this.oldvalue) {
    // do something
  }
}
谎言月老 2024-08-25 19:31:34

您可以存储旧值并检测它是否发生变化:

function keyUpHandle(e) {
  if (this.prevValue && this.prevValue != this.value) {
    // do something
  }
  this.prevValue = this.value;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文