使用select2()在下拉列表中显示所选选项

时间:2017-01-03 07:56:17

标签: jquery angularjs select2 angularjs-select2

我有两个下拉选择框,我使用AngularJS在下拉列表中显示选项。

在我的脚本中,当我使用$(".select2_demo_1").select2();时,只有一个下拉列表一次显示所选的选项。我想显示两个下拉菜单以显示所选的选项。请帮助。

<select id="one" class="select2_demo_1 form-control" ng-model="orgs" ng-options="item.OrganizationTypeTitle for item in organization"></select>
<select id="two" class="select2_demo_1 form-control" ng-model="industry" ng-options="i.IndustryTypeTitle for i in industries"></select>

var getOrganizationTypes = OrgService.getOrganizationTypes();

getOrganizationTypes.then(function (response) {
    $scope.organization = response.data;
    $scope.orgs = $scope.organization[0];

}, function () {
    alert('can not get Organization Types!');
});

var getIndustryTypes = OrgService.getIndustryTypes();

getIndustryTypes.then(function (response) {

    $scope.industries = response.data;
    $scope.industry = $scope.industries[0];

}, function () {

    alert('can not get industry types');

})

1 个答案:

答案 0 :(得分:0)

你需要一个循环来从同一个css获取价值

      $(function () { 
        var v = [];
        $(".select2_demo_1").each(function(){
            v.push($(this).val());
        })  
      });

示例:Demo On jsfiddle

<link href="http://ivaynberg.github.com/select2/select2-3.2/select2.css" rel="stylesheet"/>
<script src="http://ivaynberg.github.com/select2/select2-3.2/select2.js"></script>    


        <select  class="select2_demo_1" name="drp_Books_Ill_Illustrations" >
            <option value="1" selected>1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>



        <select  class="select2_demo_1" name="drp_Books_Ill_Illustrations" >
            <option value="1">1</option>
            <option value="2" selected>2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>


<button class="getValue">Get Value</button>
<button  class="setValue"> Set  value </button>



$(".getValue").click(function() {
           var v = [];
        $(".select2_demo_1").each(function(){       
            v.push($(this).val());
            alert($(this).val())
        })  
    });