简体   繁体   English

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

[英]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.

相关问题 如何在按键事件期间通过 jQuery 获取 HTML 文本输入的 NEW 值? - How can I get the NEW value of an HTML text input during a keypress event via jQuery? 如何检查文本字段值的格式是否正确? - How can I check if a text field value has the correct format? 如何在文本字段输入上执行javascript函数并用新值更新字段 - How can I execute a javascript function on text field input and update the field with new value 如何检查js上的输入字段并根据文本值编写和回答 - How can I check the input field on js and write and answer based on the text value 如何检查输入字段的文本是“是”还是“否”? - How can I check if the text of an input field is “yes” or “no”? 如何使用 prompt-sync 获取 1 keypress 的值? - How can I use prompt-sync to get the value of 1 keypress? 如何通过事件侦听器将按键值传递给 function? - How can I pass a keypress value to a function, through an event listener? Keypress到文本字段的任何地方? - Keypress anywhere to text field? 如何在javascript中的按键事件后检查文本输入的值是否为空? - how to check if the value of a text input is empty right after the keypress event in javascript? 如何更改输入数值的文本字段大小? - How can I change the text field size of number value entered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM