【问题标题】:Show Ionic Popup after exceeding maximum number of digits in a textbox超过文本框中的最大位数后显示 Ionic Popup
【发布时间】:2018-02-04 16:34:48
【问题描述】:

我目前正在使用 Ionic1 和 AngularJS 开发一个基本的计算器。我想将屏幕上输入的位数限制为 15 位。我想向用户显示一个离子弹出窗口。 我正在使用数字键(按钮)在我的计算器中输入数字。我该怎么做?

【问题讨论】:

  • 你能不能不做一个 maxlength="15" 所以它永远不会超过 15?
  • 我可以设置但我也想显示一个弹出窗口

标签: javascript angularjs ionic-framework popup calculator


【解决方案1】:

您可以在控制器中使用 ng-change 并发送 ng-model 值,在控制器中检查输入的长度,一旦它等于 15,您就可以显示弹出窗口

【讨论】:

    【解决方案2】:

    您可以使用$scope.$watch查看您的模型值。

    view.html

    <input type="text" ng-model="inputValue">
    

    controller.js

    $scope.$watch('inputValue', function(newValue, oldValue) {
        if(newValue.length === 15) {
            showPopup(); // your function to show popup
        }
    });
    

    【讨论】:

      【解决方案3】:

      HTML:

      <input type="input" id="myinput1" value="0" size="15" maxlength="15" />
      

      JS:

      $(document).ready(function(){
          $('[id^=myinput1]').keypress(validateNumber);
      });
      $( "#myinput1" ).on('input', function() {
          if ($(this).val().length>=15) {
              alert('show pop up');       
          }
      });
      function validateNumber(event) {
          var key = window.event ? event.keyCode : event.which;
          if (event.keyCode === 8 || event.keyCode === 46) {
              return true;
          } else if ( key < 48 || key > 57 ) {
              return false;
          } else {
              return true;
          }
      };
      

      JSFiddle Demo

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多