javascript 离子/如何从选择控件中选择多个选项(最多只能选择 3 个选项)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30566548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:22:35  来源:igfitidea点击:

Ionic /How to select multiple options from select control(Maximum selection will 3 options only)?

javascriptangularjsangularjs-ng-repeationic-frameworkionic

提问by Arul

I'm using the Ionic 1.3.16 version currently. Here I need to select multiple options in my select control.

我目前使用的是 Ionic 1.3.16 版本。这里我需要在我的选择控件中选择多个选项。

Here my ionic HTML code:

这是我的离子 HTML 代码:

<div class="list">
    <label class="item item-input item-select">
        <div class="input-label">
            Lightsaber
        </div>
        <select>
            <option>Blue</option>
            <option selected>Green</option>
            <option>Red</option>
        </select>
    </label>
</div>

采纳答案by Pankaj Parkar

You are missing the valueattribute in selectoption, because when you select option it will reflect to the ng-model.Additionally to select multiple you need to add multipleattribute in your select.

您缺少选项中的value属性select,因为当您选择选项时,它会反映到ng-model.Additionally 选择多个您需要multiple在选择中添加属性。

Markup

标记

<select ng-model="selectedValues" multiple>
    <option ng-repeat="option in options" value="{{option.value}}">{{option.name}}</option>
</select>
{{selectedValues}}

回答by Danish Bhayani

Just add the multipleattribute in the select field.

只需multiple在选择字段中添加属性。

<div class="list">
    <label class="item item-input item-select">
   <div class="input-label">
       Lightsaber
   </div>
   <select multiple="multiple">
        <option>Blue</option>
        <option selected>Green</option>
        <option>Red</option>
   </select>
</label>

回答by Mohamed Selim

use ng-options to bind data, here is how

使用 ng-options 绑定数据,方法如下

in the controller

在控制器中

$scope.values=  [
   {id:1, name:"value1" }, 
   {id:2, name:"value2" }, 
   {id:3, name:"value3" }
   ];
$scope.selectedValues= []; //initial selections

and in the view

并且在视图中

<label class="item item-input item-select">

     <select multiple ng-model='selectedValues'
                    ng-options="a.name for a in values" >
     </select>
</label>
{{selectedValues}} <!-- to preview the selection -->