Javascript onkeypress()未调用函数
我的任务是不让用户在textArea中键入“%”,因此我这样做了 但在文本区域内单击后,仍然可以键入“%”。。。这意味着,onkeypress无法识别我的函数,或者函数本身是错误的Javascript onkeypress()未调用函数,javascript,angularjs,function,controller,textarea,Javascript,Angularjs,Function,Controller,Textarea,我的任务是不让用户在textArea中键入“%”,因此我这样做了 但在文本区域内单击后,仍然可以键入“%”。。。这意味着,onkeypress无法识别我的函数,或者函数本身是错误的 $scope.test=函数(){ var txtarea=document.getElementById(“exampleFormControlTextarea1”); addEventListener(“输入”,函数(){ txtarea.value=txtarea.value.replaceAll(“%”,“”
$scope.test=函数(){
var txtarea=document.getElementById(“exampleFormControlTextarea1”);
addEventListener(“输入”,函数(){
txtarea.value=txtarea.value.replaceAll(“%”,“”);
})
}
我也试过这样做:
函数myfunction(){
var txtarea=document.getElementById(“exampleFormControlTextarea1”);
addEventListener(“输入”,函数(){
txtarea.value=txtarea.value.replaceAll(“%”,“”);
})
}
证明正当
来自此帖子:
将过滤器与ng change功能一起使用,如:
angular.module("test", [])
.filter("purger", function() {
return function(input) {
return input.replace(/%/gi, "");
}
}).controller("testController", function($scope, purgerFilter) {
$scope.onChange = function() {
$scope.negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial = purgerFilter($scope.negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial);
}
})
还有你的元素:
<textarea ...
ng-model="negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial"
ng-change="onChange()"></textarea>
我相信你要找的就在这里 你可以像这样在ngKeypress上调用你的函数。我冒昧地将您的ng模型对象重命名为model,因为您的模型太长了
<div class="col-md-12 label-base">
<label for="exampleFormControlTextarea1">Justify</label>
<textarea style="resize: none" ng-disabled="negociacaoEspecCtrl.proposta.flagDesabilitaEdicaoProposta"
class="form-control observacoes" id="exampleFormControlTextarea1" rows="3"
ng-model="model"
ng-keypress="test()"></textarea>
</div>
不要在已经是事件侦听器的函数中添加事件侦听器。。。我删除了“txtarea.addEventListener”(“input”,function()”,只让“txtarea.value=txtarea.value.replaceAll”(“%”,“);”?@PedroPastori我还删除了
txtarea.addEventListener…
。这不是AngularJS真正的做事方式。这回答了你的问题吗?
$scope.test = function() {
console.log("Method called");
$scope.model = $scope.model.replaceAll("%", "");
}