I have a bunch of NumberTextBoxes which I created programmatically. Now, I have to compute some values on keypress so I call a function for that... Looks something like this:
...
<head>
...
function setNumTextBox() {
...
var val = {
name: "mytextbox",
onKeyPress: keyPressFxn
}
new dijit.form.NumberTextBox(val, "mytextbox");
}
function keyPressFxn(evt) {
// do the processing here
}
...
Now, my problem is this. The user types, say, "12". When the user types in "1", dijit.byId("mytextbox").attr("value") is "". When the user types in the "2", the value is now "1". In short, the value (I've also tried the "displayedValue" attribute) that I get in the function is not the latest.
I tried to use "intermediateChanges: true" but it doesn't work for the text box for this case. Now, I went ahead and used the "onChange" event instead of "onKeyPress". I get the correct values but... I don't know what called the onChange event.
Questions:
If I am to use the "onKeyPress" event, how do I get the latest value? Meaning, if I type in "123", I'll get "123" when I access the field's value/displayedValue.
If I use "onChange", how do I get the id of the element that fired the onChange event? (it's not as simple as evt.target.id right? Tried that one and it didn't work)
Or, do I have to use a combination of the 2? Like, get the id of the caller via onKeyPress then get the updated val开发者_如何转开发ue that the user typed via onChange?
Thanks!
This should get you close to what you wanted. You may need to track the dojo.keys.CLEAR and dojo.keys.DELETE constants as well.
function keyPressFxn(evt) {
var value = this.value;
if (dojo.keys.BACKSPACE == evt.keyCode ) {
console.log(value.substr(0, value.length - 1));
} else {
console.log(value + String.fromCharCode(evt.charCode));
}
}
I would seriously consider using the onkeyup event instead. found this in another stackoverflow answer:
Because you are using key-press event. key press has 3 phase:
- Key down: when key is press
- Key hold: key is hold down
- Key up: key is release In your case, problem can be solved by using keyup event
Not sure if this just because the API has changed in the last 2 years - but:
mytextbox.get("value")
gives the up to date value as the event fires
(BEWARE tho: mytextbox.value
doesn't give the latest value until after the textbox has been validated)
精彩评论