简体   繁体   English

Dojo按键事件无效

[英]Dojo key press event is not working

I am using dojo to disable other keypress events on dojo.form.numberTextBox . 我正在使用dojo来禁用dojo.form.numberTextBox上的其他按键事件。 I am doing in this way: 我这样做:

<input style="width: 100px" data-dojo-type="dijit.form.NumberTextBox"  
  name="test" id="test" maxlength="3">

And using the script: 并使用脚本:

require(["dojo/keys", "dojo/on"], 
    dojo.connect(dijit.byId("remainderDays"), "onKeyPress", function (evt) {
    var charOrCode = evt.charCode || evt.keyCode;
    if (charOrCode == keys.NUMPAD_0) {
        dojo.stopEvent(evt);
    }
})); 

Its not working. 它不起作用。

Even the Javascript function to disable keypress events except numbers is not working. 甚至用于禁用除数字之外的按键事件的Javascript函数也不起作用。 But when I remove dojo type from input, it starts working. 但是当我从输入中删除dojo类型时,它开始工作。

Any idea or help would be appreciated. 任何想法或帮助将不胜感激。

Well, you placed the dojo.connect wrong and if I understood well, you're trying to block all keys except the numbers. 好吧,你把dojo.connect放错了,如果我理解得很好,你就试图阻止除数字之外的所有键。 If you want that, you should check for something like: 如果你想要,你应该检查以下内容:

if (evt.charOrCode > '9' || evt.charOrCode < '0') {
    ...
}

The code itself looks like: 代码本身看起来像:

require(["dijit/form/NumberTextBox"]);
require(["dojo/ready", "dojo/parser", "dojo/on", "dojo/keys"], function(ready, parser, on, keys) {
    ready(function() {
        parser.parse();

        on(dijit.byId("test"), "keypress", function(evt) {
             if (evt.charOrCode > '9' || evt.charOrCode < '0') {
                 dojo.stopEvent(evt);
             }
        });
    });
});

As you can see I removed the dojo.connect (because it's deprecated) and I used the "keypress" event. 正如您所看到的,我删除了dojo.connect (因为它已被弃用)并且我使用了"keypress"事件。 I also fixed your code (because your syntax was wrong). 我还修复了你的代码(因为你的语法错了)。

A working JSFiddle can be found here . 可以在这里找到一个工作的JSFiddle。

You can add the following line instead of using key press event: 您可以添加以下行而不是使用按键事件:

var btnClick = dijit.byId("test")._onKey = function(evt) {
 key = evt.keyCode;
 if (key == dojo.keys.ENTER) {
  //what you want it to do
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM