【问题标题】:Get the value of dynamic check boxes in angular js when submitting the form提交表单时获取angular js中动态复选框的值
【发布时间】:2017-01-26 18:50:59
【问题描述】:

在我的 HTML 表单中有 5 个复选框。如何在提交表单时检查哪些复选框已选中?

【问题讨论】:

    标签: angularjs forms checkbox


    【解决方案1】:

    我正在使用这段小代码。随意拿走。

    在您的控制器中:

    $scope.sentinel = [];
    
    $scope.toggleSelection = function(value) {              
                // Bit trick, equivalent to "contains"
                if (~$scope.sentinel.indexOf(value)) {
                    var idx = $scope.sentinel.indexOf(value);
                    $scope.sentinel.splice(idx, 1);
                    return;
                }
    
                $scope.sentinel.push(value);
            };
    

    然后在你的 HTML 中:

    <span ng-repeat="key in $scope.yourarray">
        <md-checkbox style="margin-left:30px;" aria-label="Select item"
        ng-click="toggleSelection(key)"><span>{{ key }}</span></md-checkbox>
        <br/>
    </span>
    

    这允许您拥有任意大小的数组并使用sentinel 数组来注册已检查的值。如果您再次选中某个框,toogleSelection 将阻止您再次添加值。

    【讨论】:

      【解决方案2】:

      您可以使用 Angular.js 的Checklist-model。很有用:

      var app = angular.module("app", ["checklist-model"]);
      app.controller('Ctrl1', function($scope) {
        $scope.roles = [
          'guest', 
          'user', 
          'customer', 
          'admin'
        ];
        $scope.user = {
          roles: ['user']
        };
        $scope.checkAll = function() {
          $scope.user.roles = angular.copy($scope.roles);
        };
        $scope.uncheckAll = function() {
          $scope.user.roles = [];
        };
        $scope.checkFirst = function() {
          $scope.user.roles.splice(0, $scope.user.roles.length); 
          $scope.user.roles.push('guest');
        };
        $scope.showCheckedOnes = function() {
          var checkedBoxes = "";
          for (var i = 0; i < $scope.user.roles.length; i++) {
            checkedBoxes += $scope.user.roles[i] + " ";
          }
          alert(checkedBoxes);
        };
      });
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
      <script src="https://vitalets.github.io/checklist-model/checklist-model.js"></script>
      
        <div ng-app="app">
          <div ng-controller="Ctrl1">
      
            <label ng-repeat="role in roles">
              <input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
            </label>
      
          <div>
        <div>
        <button ng-click="checkAll()" style="margin-right: 10px">Check all</button>
        <button ng-click="uncheckAll()" style="margin-right: 10px">Uncheck all</button>
        <button ng-click="checkFirst()" style="margin-right: 10px">Check first</button>
        <button ng-click="showCheckedOnes()" style="margin-right: 10px">Show checked ones</button>

      【讨论】:

      • 感谢您抽出宝贵的时间来回答我的问题。
      【解决方案3】:

      您可以定义数组值并映射到html。

      $scope.checkbox = [];
      

      HTML

      <input ng-model="checkbox[0] type="checkbox" />
      <input ng-model="checkbox[1] type="checkbox" />
      <input ng-model="checkbox[2] type="checkbox" />
      <input ng-model="checkbox[4] type="checkbox" />
      

      或者您可以将复选框定义为对象并将复选框[索引]更改为复选框[名称]。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-23
        • 2016-12-06
        相关资源
        最近更新 更多