【问题标题】:need to get value of checkbox value in html to controller in angular js需要将html中复选框值的值获取到角度js中的控制器
【发布时间】:2016-05-30 13:25:39
【问题描述】:

我有复选框,我需要在控制器功能中的按钮提交上获取该复选框的值:

 <input id="Check" type="checkbox" ng-model="CheckValue" />

【问题讨论】:

    标签: javascript jquery html angularjs


    【解决方案1】:

    $scope.CheckValue 将在您的控制器中具有值。

    在提交时执行console.log($scope.CheckValue),您将看到console 中的值。

    【讨论】:

    • 谢谢。这对我帮助很大。
    • @Ervikas。如果它解决了您的问题,您可以接受答案。
    【解决方案2】:

    在你应该使用的控制器中

    $scope.CheckValue
    

    有时在控制器中定义您在模板中使用的属性也很好。因此,您将对变量有更清晰的概览和控制。

    【讨论】:

      【解决方案3】:
      function forceNumber(element) {
        element
          .data("oldValue", '')
          .bind("paste", function(e) {
            var validNumber = /^[-]?\d+(\.\d{1,2})?$/;
            element.data('oldValue', element.val())
            setTimeout(function() {
              if (!validNumber.test(element.val()))
                element.val(element.data('oldValue'));
            }, 0);
          });
        element
          .keypress(function(event) {
            var text = $(this).val();
            if ((event.which != 46 || text.indexOf('.') != -1) && //if the keypress is not a . or there is already a decimal point
              ((event.which < 48 || event.which > 57) && //and you try to enter something that isn't a number
                (event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a -
                (event.which != 0 && event.which != 8))) { //and the keypress is not a backspace or arrow key (in FF)
              event.preventDefault(); //cancel the keypress
            }
      
            if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2) && //if there is a decimal point, and there are more than two digits after the decimal point
              ((element[0].selectionStart - element[0].selectionEnd) == 0) && //and no part of the input is selected
              (element[0].selectionStart >= element.val().length - 2) && //and the cursor is to the right of the decimal point
              (event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a -
              (event.which != 0 && event.which != 8)) { //and the keypress is not a backspace or arrow key (in FF)
              event.preventDefault(); //cancel the keypress
            }
          });
      }
      
      forceNumber($("#myInput"));
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <input type="text" id="myInput" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 2018-10-06
        • 2015-10-29
        • 2020-03-03
        相关资源
        最近更新 更多