使用OnkeyDown作为if条件Javascript

时间:2017-07-04 09:15:20

标签: javascript angularjs

我想使用.onkeydown来限制我的输入字段。我写了这个函数,但是我想在if condition中使用它来检查输入键是对还是错。

但是我不知道我应该如何在我的if中使用这种形式来调用这个函数,因为它实际上并不是一个Angular范围函数,而是一个JS函数... < / p>

&#13;
&#13;
var cpNumber = document.getElementById("cpNumber");                 
cpNumber.onkeydown = function(e) {
    if((e.keyCode > 95 && e.keyCode < 106)
        || (e.keyCode > 47 && e.keyCode < 58)
        || ((e.keyCode > 95 && e.keyCode < 106)
        || (e.keyCorde == 65)) 
        || ((e.keyCode > 47 && e.keyCode < 58) 
        || (e.keyCode == 65))
        || ((e.keyCode > 95 && e.keyCode < 106) 
        || (e.keyCorde == 66)) 
        || ((e.keyCode > 47 && e.keyCode < 58)
        || (e.keyCode == 66))
        || e.keyCode == 8) {
        return true;
    } else {
        return false;
    }
};
                        
                        
// If condition, check if entry keys are wrong. 
// I tried cpNumber.onkeydown(e), but the "e" 
// seems drop from the sky, it always shows "false".
    
if(cpNumber.onkeydown(e) === false) {
    alertPopup = $ionicPopup.alert({
        title: 'Oups...',
        cssClass: 'pop',
        template: '<div class="center-form">This is Wrong !</div>'
    });
}        
  
&#13;
<form>
    <input class="input" 
           type="text" 
           id="cpNumber" 
           maxlength="5" 
           ng-model="cp" 
           placeholder="CP">
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您需要将调用放入块本身。这是你要看的小提琴:

https://jsfiddle.net/tsamzx6h/

                          if((e.keyCode > 95 && e.keyCode < 106)
                              || (e.keyCode > 47 && e.keyCode < 58)
                              || ((e.keyCode > 95 && e.keyCode < 106)
                              || (e.keyCorde == 65)) 
                              || ((e.keyCode > 47 && e.keyCode < 58) 
                              || (e.keyCode == 65))
                              || ((e.keyCode > 95 && e.keyCode < 106) 
                              || (e.keyCorde == 66)) 
                              || ((e.keyCode > 47 && e.keyCode < 58)
                              || (e.keyCode == 66))
                              || e.keyCode == 8)
                            {
                                alert("Good!");
                            }
                            else
                            {
                                alert("Wrong");
                            }

答案 1 :(得分:1)

不能以这种方式调用事件。为了使您的程序正常运行,您可以在else块中添加一个函数并调用它并执行您想要的任何操作。 加上你也可以通过你的活动来运作。这将成为一个额外的优势。 HTML:

 <form><input class="input" type="text" id="cpNumber" maxlength="5" ng-model="cp" placeholder="CP"></form>

脚本::

ar cpNumber = document.getElementById("cpNumber");


                        cpNumber.onkeydown = function(e) {

                                if((e.keyCode > 95 && e.keyCode < 106)
                                  || (e.keyCode > 47 && e.keyCode < 58)
                                  || ((e.keyCode > 95 && e.keyCode < 106)
                                  || (e.keyCorde == 65)) 
                                  || ((e.keyCode > 47 && e.keyCode < 58) 
                                  || (e.keyCode == 65))
                                  || ((e.keyCode > 95 && e.keyCode < 106) 
                                  || (e.keyCorde == 66)) 
                                  || ((e.keyCode > 47 && e.keyCode < 58)
                                  || (e.keyCode == 66))
                                  || e.keyCode == 8)
                                {
                                    return true;
                                }
                                else
                                {
                                    notAllowed(e);
                                }
                        };


        // If condition, check if entry keys are wrong. I tried cpNumber.onkeydown(e), but the "e" seems drop from the sky, it always shows "false".

                            function notAllowed (ev) {
                               alertPopup = $ionicPopup.alert({
                                    title: 'Oups...',
                                    cssClass: 'pop',
                                    template: '<div class="center-form">This is Wrong !</div>'
                                });
                            }