简体   繁体   中英

Dojo key press event is not working

I am using dojo to disable other keypress events on 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. But when I remove dojo type from input, it starts working.

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. 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. I also fixed your code (because your syntax was wrong).

A working JSFiddle can be found here .

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
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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