【问题标题】:How do you get an array of selects values from multiple select boxes?如何从多个选择框中获取选择值数组?
【发布时间】:2014-09-27 08:49:53
【问题描述】:

我正在尝试获取表格中动态生成的选择框的值数组。您如何获得这些值的数组或其他数据类型?

这是表格:

  <table>
    <thead>
      <tr>
        <th>Personal Action Field(from Infor-Lawson)</th>
        <th>Form Fields</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="action in actionFields">
        <th>{{action.actionParam}} </th>
        <th>
          <select ng-model="name" ng-options="item.formField for item in formFields"></select>
          Currently selected: {{ name }}
        </th>
      </tr>
    </tbody>
  </table>
  <br><br>
  <button ng-click="lastPage()">Back</button> 
  <button ng-click="nextPage(); mapValues()">Next</button> 

这里是控制器:

function FormActionFieldController($scope, $http, $rootScope) {
  $rootScope.data = null;

  $http.get('http://localhost:82/rs/transformTrigger/formFields')
    .success(function (data, status, headers, config) {
      $rootScope.formFields = data;
  })
  .error(function (data, status, headers, config) {
     //  Do some error handling here
    alert('error FormActionFieldController');
  });
  $rootScope.getFields=function(){  
    $scope.actionFields = null;
    $scope.httpData = '{"actionName":"'+$rootScope.actionTypeName+'"}';

  $http.post('http://localhost:82/rs/transformTrigger/lawsonFields', $scope.httpData)
      .success(function (data, status, headers, config) {
       $scope.actionFields = data;
  })
  .error(function (data, status, headers, config) {
      //  Do some error handling here
  });
  }
  $scope.nextPage=function(){
      $rootScope.page3=false;
      $rootScope.page4=true;
  }
  $scope.lastPage=function(){
    $rootScope.page3=false;
    $rootScope.page2=true;
  }
 $scope.mapValues=function(){
    $rootScope.submitArray = $rootScope.formFields;
    alert($rootScope.submitArray);

  }
 }

【问题讨论】:

  • 什么看起来像一个formField?如果您有 id 或名称,则可以使用 ng-model=datas[name] 存储数据。使用它,您将获得一个包含选定数据的对象,您可以对其进行迭代以获得所需的数组。

标签: angularjs select html-table angularjs-ng-change


【解决方案1】:

你可以使用 $index

<select ng-model="name[$index]" ng-options="item.formField for item in formFields"></select>

这样你得到一个选定值的数组

【讨论】:

  • 当我添加 $index 时,我得到这个错误:TypeError: Cannot set property '0' of undefined。有什么想法吗?
  • 请尝试初始化名称。 $ scope.name = []
猜你喜欢
  • 2018-09-19
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
相关资源
最近更新 更多