keydown事件的新价值

问题描述 投票:8回答:4

浏览器:Google Chrome V19.0.1084.52

我有一个文本框需要是一个小于或等于255的数字,在keydown我想检查这个值是否大于或等于255否则阻止该事件。

在控制台中,当我在console.log事件时,它将显示event.srcElement.value,因为该值将出现,即从12 => 123,当我console.log时,event.srcElement.value将显示为输入,而不是将会。

Console.log一个接一个地发生,两者之间没有任何停顿。

如何在keydown上找到文本框的新值以及为什么console.log会返回不同的结果?

谢谢

这是我的代码:

function inputNumeric(event,max) {

    console.log (event);
    console.log ('event.originalEvent.srcElement.value: '+event.originalEvent.srcElement.value);
    console.log ('event.srcElement.value: '+event.srcElement.value);

}

$("#rs485addr").keydown(function(event) {
    inputNumeric(event);
});

CONSOLE.LOG:

event.originalEvent.srcElement.value: 12
event.srcElement.value: 12

event.srcElement:

accept: ""
accessKey: ""
align: ""
alt: ""
attributes: NamedNodeMap
autocomplete: ""
autofocus: false
baseURI: "http://10.50.50.60/controller/?bid=10"
checked: false
childElementCount: 0
childNodes: NodeList[0]
children: HTMLCollection[0]
classList: DOMTokenList
className: "width-60"
clientHeight: 18
clientLeft: 2
clientTop: 2
clientWidth: 62
contentEditable: "inherit"
dataset: DOMStringMap
defaultChecked: false
defaultValue: "1"
dir: ""
dirName: ""
disabled: false
draggable: false
files: null
firstChild: null
firstElementChild: null
form: null
formAction: ""
formEnctype: "application/x-www-form-urlencoded"
formMethod: "get"
formNoValidate: false
formTarget: ""
hidden: false
id: "rs485addr"
incremental: false
indeterminate: false
innerHTML: ""
innerText: ""
isContentEditable: false
jQuery17102950612970162183: 22
labels: NodeList[1]
lang: ""
lastChild: null
lastElementChild: null
localName: "input"
max: ""
maxLength: 3
min: ""
multiple: false
name: ""
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: null
nextSibling: Text
nodeName: "INPUT"
nodeType: 1
nodeValue: null
offsetHeight: 22
offsetLeft: 183
offsetParent: HTMLBodyElement
offsetTop: 365
offsetWidth: 66
onabort: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onblur: null
onchange: null
onclick: null
oncontextmenu: null
oncopy: null
oncut: null
ondblclick: null
ondrag: null
ondragend: null
ondragenter: null
ondragleave: null
ondragover: null
ondragstart: null
ondrop: null
onerror: null
onfocus: null
oninput: null
oninvalid: null
onkeydown: null
onkeypress: null
onkeyup: null
onload: null
onmousedown: null
onmousemove: null
onmouseout: null
onmouseover: null
onmouseup: null
onmousewheel: null
onpaste: null
onreset: null
onscroll: null
onsearch: null
onselect: null
onselectstart: null
onsubmit: null
onwebkitfullscreenchange: null
onwebkitfullscreenerror: null
onwebkitspeechchange: null
outerHTML: "<input class="width-60" id="rs485addr" maxlength="3" type="textbox" value="1">"
outerText: ""
ownerDocument: HTMLDocument
parentElement: HTMLSpanElement
parentNode: HTMLSpanElement
pattern: ""
placeholder: ""
prefix: null
previousElementSibling: HTMLLabelElement
previousSibling: Text
readOnly: false
required: false
scrollHeight: 16
scrollLeft: 0
scrollTop: 0
scrollWidth: 60
selectionDirection: "forward"
selectionEnd: 3
selectionStart: 3
size: 20
spellcheck: true
src: ""
step: ""
style: CSSStyleDeclaration
tabIndex: 0
tagName: "INPUT"
textContent: ""
title: ""
translate: true
type: "text"
useMap: ""
validationMessage: ""
validity: ValidityState
value: "123"
valueAsDate: null
valueAsNumber: NaN
webkitGrammar: false
webkitRegionOverflow: "undefined"
webkitSpeech: false
webkitdirectory: false
webkitdropzone: ""
willValidate: true
javascript javascript-events keydown
4个回答
25
投票

我不确定它是否有任何帮助,但当我不得不在事件监听器中处理类似的情况时,我使用了qmsxswpoi,1ms超时,我将主要功能检查值,等等。

这是因为当setTimeout()事件被触发时,输入字段尚未填充新数据。

简单的jQuery示例:

keydown

UPDATE

在现代浏览器中使用'input'事件。

$('#input').on('keydown', function(e) {
  var field = $(this);
  var prevValue = field.val();
  setTimeout(function() {
    // check if new value is more or equal to 255
    if (field.val() >= 255) {
      // fill with previous value
      field.val(prevValue);
    }

  }, 1);
});

10
投票

你不应该使用var prevValue = 0; $('#input').on('input', function(e) { var field = $(this); // check if new value is more or equal to 255 if (field.val() >= 255) { // fill with previous value field.val(prevValue); } else { // If value is lower than 255 set it in prev value. prevValue = field.val(); } }); keydown。然后,您将收到要键入的字符的实际字符代码。

keypresskeypress, keydown, keyup value of input box AFTER event,尤其是http://www.quirksmode.org/dom/events/keys.html

http://www.quirksmode.org/js/keys.html

如果您只想在输入内容后获取输入值,则需要使用inputElement.addEventListener("keypress", function(e) { var curval = e.srcElement.value; var newchar = String.fromCharCode(e.charCode || e.keyCode); if (/* condition */) e.preventDefault(); }, false); 事件。


0
投票

我唯一能想到的是,在控制台中输出的srcElement对象仍然链接到事件Object,即使在keydown事件之后也是如此,以便控制台中的Object从12更新为123,即使已经在安慰。

event.srcElement.value的直接输出不会发生这种情况,因为这是一个文字值,并在日志记录中复制,未引用...

如果你真的很快你可以检查你是否可以在控制台中看到它从12变为123 ;-)


0
投票

在这里添加@ Bergi的评论,以便包括文本选择和插入位置,您可以按如下方式进行:

keyup
© www.soinside.com 2019 - 2024. All rights reserved.