【问题标题】:How to select multiple selected value from select option如何从选择选项中选择多个选定值
【发布时间】:2023-04-08 09:04:01
【问题描述】:

我的控制器代码看起来像

$scope.items = [{
    heading: 'Sports',
    types: [{
        name: 'Football',
    }, {
        name: 'Persie',
    }, {
        name: 'Ronaldo',
    }, {
        name: 'Messy',
    }],
    id: '1'
}, {
    heading: 'Cricket',
    types: [{
        name: 'Tendulkar',
    }, {
        name: 'Lara',
    }, {
        name: 'Ponting',
    }],
    id: '2'
}];

我的视图包含这样的内容: 用户单击提交按钮时如何获取选项的选定值

【问题讨论】:

  • searchOption 在显示和更改选项值时呈现什么?
  • ng-model 将被设置为所选项目
  • 我仍然无法在 myCtrl 中查看

标签: angularjs model-view-controller ionic-framework ionic


【解决方案1】:

这里是jsfiddle 我使用 ng-repeat 构建选择和 ng-options 来填充它们,然后您必须使用相对 ng-model 来获取选择。

HTML:

<div ng-app ng-controller="MyCtrl">
  <select class="select fancy" ng-repeat="(i, item) in items" ng-model="searchOption[i]" ng-options="type.name for type in item.types"></select>
  <button ng-click="submitIt()">Submit</button>
</div>

Javascript:

function MyCtrl($scope) {
    $scope.submitIt = function () {
        console.log($scope.searchOption);
    };

    $scope.searchOption = [];

    $scope.items = [{
            heading: 'Sports',
            types: [{
                name: 'Football',
    }, {
                name: 'Persie',
    }, {
                name: 'Ronaldo',
    }, {
                name: 'Messy',
    }],
            id: '1'
  }, {
            heading: 'Cricket',
            types: [{
                name: 'Tendulkar',
    }, {
                name: 'Lara',
    }, {
                name: 'Ponting',
    }],
            id: '2'
  }];
}

【讨论】:

  • 有 2 个选项框不是 1 ,我想要两者的选定值
  • 所以你应该写得更好你的代码,另一个选择在哪里?你是否使用 ng-repat 而不是 $scope.items
  • 对于每个标题,我都有一个带有相应类型作为选项值的选项,并且我在此 div 上方使用 ng-repeat
  • 更新答案请看
  • 坚如磐石 :) 非常感谢
猜你喜欢
  • 2010-10-06
  • 1970-01-01
  • 2022-07-16
  • 1970-01-01
  • 2015-07-24
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 2015-02-19
相关资源
最近更新 更多