简体   繁体   English

在 angular6 中禁用按键输入?

[英]Disable input on keydown in angular6?

I am trying to set attribute disable to that input only on which keydown event occured and remaining input should not be disabled?我试图仅在发生 keydown 事件时将attribute disable设置为该输入,并且不应禁用剩余的输入?

abc.component.html abc.component.html

<input type="text" (keydown)="disableIt($event)"> // <-- If keydown occurs 
                                                   //here, disable it
<input type="text" (keydown)="disableIt($event)">
<input type="text" (keydown)="disableIt($event)">

abc.component.ts abc.component.ts

public disableIt(evt) {
   if (evt.keyCode === 13 || evt.keyCode === 9) {
       evt.target.tagName.disabled  = true;
    }
}

关于什么

<input type="text" (keydown)="$event.preventDefault();">

You can use this controller:您可以使用此控制器:

ctrl($scope) {
    $scope.inputDisabled = [];
    $scope.inputs = [];
    for(let i = 0; i < numInputs; i++) {
        $scope.inputDisabled.push(false);
        $scope.inputs.push(i);
    }
    $scope.disableInput(which) {
        for(let i = 0; i < numInputs; i++) {
            $scope.inputDisabled.push(false);
        }
        $scope.inputDisabled[which] = true;
    }
}

And then in HTML:然后在 HTML 中:

<input ng-repeat="i in inputs" ng-disabled="inputDisabled[i]" ng-keydown="disableInput(i)">

You select the element incorrectly and you should use which instead of keyCode.您错误地选择了元素,您应该使用 which 而不是 keyCode。

 function disableIt(event) { var which = event.which if (which===13 || which===9) { event.target.disabled = true } }
 <input type="text" onkeydown="disableIt(event)"> <input type="text" onkeydown="disableIt(event)"> <input type="text" onkeydown="disableIt(event)">

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

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