Dojo:如何在 onKeyPress 或 onChange 上获取 NumberTextBox 中的最新值?
我有一堆以编程方式创建的 NumberTextBox。现在,我必须计算按键上的一些值,因此我为此调用一个函数...看起来像这样:
...
<head>
...
function setNumTextBox() {
...
var val = {
name: "mytextbox",
onKeyPress: keyPressFxn
}
new dijit.form.NumberTextBox(val, "mytextbox");
}
function keyPressFxn(evt) {
// do the processing here
}
...
现在,我的问题是这样的。例如,用户键入“12”。当用户输入“1”时,dijit.byId(“mytextbox”).attr(“value”)为“”。当用户输入“2”时,该值现在为“1”。简而言之,我在函数中获得的值(我也尝试过“displayedValue”属性)不是最新的。
我尝试使用“intermediateChanges:true”,但它不适用于这种情况的文本框。现在,我继续使用“onChange”事件而不是“onKeyPress”。我得到了正确的值,但是...我不知道什么叫 onChange 事件。
问题:
如果我要使用“onKeyPress”事件,如何获取最新值?这意味着,如果我输入“123”,当我访问该字段的值/displayedValue 时,我将得到“123”。
如果我使用“onChange”,如何获取触发 onChange 事件的元素的 id? (它不像 evt.target.id 那么简单,对吗?尝试过,但不起作用)
或者,我必须使用两者的组合吗?比如,通过 onKeyPress 获取调用者的 id,然后获取用户通过 onChange 键入的更新值?
谢谢!
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 value that the user typed via onChange?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该会让你接近你想要的。您可能还需要跟踪 dojo.keys.CLEAR 和 dojo.keys.DELETE 常量。
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.
我会认真考虑使用 onkeyup 事件。 在另一个 stackoverflow 答案中找到了此内容 :
I would seriously consider using the onkeyup event instead. found this in another stackoverflow answer:
不确定这是否只是因为 API 在过去 2 年发生了变化 - 但是:
mytextbox.get("value")
在事件触发时提供最新值(请注意:
mytextbox.value
在验证文本框之前不会给出最新值)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)