简体   繁体   English

如何基于angularjs onchange中下拉菜单的所选选项的值在div上添加动态ID

[英]how to add dynamic id on div based on the value of selected option of dropdown in angularjs onchange

Here I have select box,onchange of dropdown I need to add dynamic id on div based on the value of selected option of dropdown .I have mentioned in alert what id to be add onchange.Here is the code below 在这里我有选择框,下拉列表的更改,我需要基于下拉列表的选定选项的值在div上添加动态id。我在警报中提到要添加onchange的id。这是下面的代码

html html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
<select class="change" ng-model="x" ng-change="update()">
<option value="city">Cities</option>
<option value="state">States</option>
<option value="country">Countries</option>
</select>
<div id=""></div>

script 脚本

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.update = function() { 
   if($scope.x == 'city'){
   alert('add id as city1 inside ablove div');
   }
   if($scope.x == 'state'){
   alert('add id as mystate inside ablove div');
   }
   if($scope.x == 'country'){
   alert('add id as mycountry inside ablove div');
   }

}
});

You can do this in your HTML: 您可以在HTML中执行此操作:

<div id="{{id}}"></div>

And you can do this in your update() function: 您可以在update()函数中执行此操作:

$scope.update = function() { 
   if($scope.x == 'city'){
       $scope.id = 'city1';
   } 
   if($scope.x == 'state'){
       $scope.id = 'mystate';
   }
   if($scope.x == 'country'){
       $scope.id = 'mycountry';
   }

}

 var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.update = function() { if ($scope.x == 'city') { $scope.selectedValue = 'cityName'; } else if ($scope.x == 'state') { $scope.selectedValue = 'stateName'; } else if ($scope.x == 'country') { $scope.selectedValue = 'countryName'; } else { $scope.selectedValue = ''; } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <select class="change" ng-model="x" ng-change="update()"> <option value="">Please Select</option> <option value="city">Cities</option> <option value="state">States</option> <option value="country">Countries</option> </select> <span ng-if="selectedValue">You have selected :- {{selectedValue}}</span> </div> 

  you need to use ngChange and pass ngModel object as argument to your ngChange function
    **Example:**
    <div ng-app="App" >
      <div ng-controller="ctrl">
        <select ng-model="blisterPackTemplateSelected" ng-change="changedValue(blisterPackTemplateSelected)" 
                data-ng-options="blisterPackTemplate as blisterPackTemplate.name for blisterPackTemplate in blisterPackTemplates">
          <option value="">Select Account</option>
        </select>
        {{itemList}}     
      </div>       
    </div>
js:
function ctrl($scope) {
  $scope.itemList = [];
  $scope.blisterPackTemplates = [{id:1,name:"a"},{id:2,name:"b"},{id:3,name:"c"}];

  $scope.changedValue = function(item) {
    $scope.itemList.push(item.name);
  }       
}
Live example: http://jsfiddle.net/choroshin/9w5XT/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在angularjs中获取下拉列表的选定选项值并在下拉列表更改时动态更改div ID - How to get selected option value of dropdown in angularjs and change the div id dynamically onchange of dropdown 根据选定的Option AngularJs将类添加到下拉列表 - Add class to dropdown based on selected Option AngularJs Angularjs Dropdown OnChange选定的文本和值 - Angularjs Dropdown OnChange Selected Text and Value 禁用动态下拉选项值 onchange another dynamic dropdown - Disable dynamic dropdown option value onchange another dynamic dropdown 如何从动态下拉选项中获取当前选定的选项值? - How to get current selected option value from dynamic dropdown option? 如何根据下拉列表中的选定选项隐藏单个 div - How to hide single div based on selected option from dropdown React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? 根据另一个下拉 onchange 函数设置下拉选项值 - set dropdown option value based another dropdown onchange function 选项选择值更改 - Option selected value onchange AngularJS在变量中获取所选下拉选项的值 - AngularJS Getting the value of a selected dropdown option in a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM