最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get selected value from select box in angularjs - Stack Overflow

programmeradmin6浏览0评论

Hello Everyone

I am trying to get option value from select box on button click but it shows undefined in console .Options value are ing from server

Here is my html code

<div class="form-group">
        <label class="control-form" for="cityid">Selct City</label>
        <select type="text" class="form-control" placeholder="City" id="acity" >
            <option value="">--Select City--</option>
            <option ng-repeat="city in cityinfo" ng-value="{{city.id}}"  ng-selected="{{city.id ==cityid}}">{{city.cityname}}</option>
</select>

<button class="btn btn-info prevnext pull-right" ng-click="nextpage()">Next <i class="fa fa-arrow-right"></i></button>

Controller.js file code

  $scope.nextpage = function(pageno) {
   console.log($scope.cityinfo);
   }

Thanks in advance

Hello Everyone

I am trying to get option value from select box on button click but it shows undefined in console .Options value are ing from server

Here is my html code

<div class="form-group">
        <label class="control-form" for="cityid">Selct City</label>
        <select type="text" class="form-control" placeholder="City" id="acity" >
            <option value="">--Select City--</option>
            <option ng-repeat="city in cityinfo" ng-value="{{city.id}}"  ng-selected="{{city.id ==cityid}}">{{city.cityname}}</option>
</select>

<button class="btn btn-info prevnext pull-right" ng-click="nextpage()">Next <i class="fa fa-arrow-right"></i></button>

Controller.js file code

  $scope.nextpage = function(pageno) {
   console.log($scope.cityinfo);
   }

Thanks in advance

Share Improve this question edited Apr 4, 2016 at 12:04 Sharma Vikram asked Apr 4, 2016 at 10:22 Sharma VikramSharma Vikram 2,4706 gold badges25 silver badges47 bronze badges 1
  • Possible duplicate of How to set a selected option of a dropdown list control using angular JS – cleftheris Commented Apr 4, 2016 at 10:28
Add a ment  | 

2 Answers 2

Reset to default 12

Use ng-options instead of ng-repeat.

Like this:

Updated

<select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="selectedCity">
    <option value="">--Select City--</option>
</select>
<button class="btn btn-info prevnext pull-right" ng-click="nextpage(selectedCity)">Next <i class="fa fa-arrow-right"></i></button>

JS:

$scope.nextpage = function(selectedCity){
    console.log(selectedCity);
}

Here is the sample code.

Html code

 <select type="text" class="form-control" placeholder="City" id="acity" ng-options="city.id as city.cityname for city in cityinfo track by city.id" ng-model="currentCity">
    <option value="">--Select Cities--</option>
</select>
 <button class="btn btn-info prevnext pull-right" ng- 
  click="nextpage(currentCity)">Next <i class="fa fa-arrow-right"></i> 
  </button>

JS function

$scope.nextpage = function(currentCity){
    console.log(currentCity);
}
发布评论

评论列表(0)

  1. 暂无评论