简体   繁体   中英

Javascript test for blur event

I'm working with knockout. i'm trying to test for enter key press OR blur event.

HTML:

<input class="percent-text" data-bind="numeric: Percent, value: Percent, event: { keypress: $root.percentUpdate, blur: $root.percentUpdate }" type="number" min="1" max="100" oninput="maxlength(this)" maxlength="3" />

knockout model:

self.percentUpdate = function (data, event) {
            if (event.keyCode === 13 || TEST FOR BLUR HERE) {
....

not sure how to test for blur event. help thanks

Why not just extra functions:

<input class="percent-text" data-bind="numeric: Percent, value: Percent, event: { keypress: $root.keypress, blur: $root.blur }" type="number" min="1" max="100" oninput="maxlength(this)" maxlength="3" />

and:

self.keypress = function(data, event){
  self.percentUpdate(data, 'keypress', event.keyCode);
};
self.blur = function(data){
  self.percentUpdate(data, 'blur');
}

self.percentUpdate = function (data, event, keyCode) {
            if (event === 'blur' && keyCode === 13) {
            ...}

Not familiar with knockout, but I assume regular JavaScript will work:

self.percentUpdate = function (data, event) {
  if (event.keyCode === 13 || event.type==='blur') {

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