javascript - 如果下拉列表与 json 的键绑定(bind),如何从下拉列表中获取选定的值

标签 javascript jquery html json angularjs

我对 Angularjs 非常陌生,我已将列名称(键值对中的键)绑定(bind)到选择列表,并希望在选择的选择更改时获取键名称。 显示选择列表:

姓名、摘要和年龄

在选择更改时,我想要选择的选定文本。请帮忙 这是我的代码:

<!DOCTYPE html>

<div ng-controller="HelloController">

    <h2>Hello {{helloTo.title}} !</h2>
    <select ng-options="key for (key,val) in phones[0]" ng-change="ChangeValue(key)" ng-model="phones"></select>
</div>


<script>
    angular.module("myapp", [])
        .controller("HelloController", function ($scope) {
            $scope.helloTo = {};
            $scope.helloTo.title = "World, AngularJS";
            $scope.phones = [
                {
                    'name': 'Nexus S',
                    'snippet': 'Fast just got faster with Nexus S.',
                    'age': 1
                },
                {
                    'name': 'Motorola XOOM™ with Wi-Fi',
                    'snippet': 'The Next, Next Generation tablet.',
                    'age': 2
                },
                {
                    'name': 'MOTOROLA XOOM™',
                    'snippet': 'The Next, Next Generation tablet.',
                    'age': 3
                }
            ];
            $scope.ChangeValue = function (selectedval) {

                alert(selectedval.name);
            };
        });
</script>

最佳答案

<select ng-options="key as key for (key,value) in phones[0]" ng-change="ChangeValue()" ng-model="selectedVal"></select>

Controller :

angular.module("myapp", [])
        .controller("HelloController", function ($scope) {
$scope.helloTo = {};
  $scope.helloTo.title = "World, AngularJS";
  $scope.selectedVal = '';
  $scope.phones = [{
    'name': 'Nexus S',
    'snippet': 'Fast just got faster with Nexus S.',
    'age': 1
  }, {
    'name': 'Motorola XOOM™ with Wi-Fi',
    'snippet': 'The Next, Next Generation tablet.',
    'age': 2
  }, {
    'name': 'MOTOROLA XOOM™',
    'snippet': 'The Next, Next Generation tablet.',
    'age': 3
  }];
  $scope.ChangeValue = function() {

    console.log($scope.selectedVal);
  };
});

就在 documentation 中其实...

这是一个plunker .

关于javascript - 如果下拉列表与 json 的键绑定(bind),如何从下拉列表中获取选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26449092/

相关文章:

javascript - 如何设计带有子列表的列表,子列表应显示在父列表的悬停上

javascript - 将图像元素添加到 <a> 标记

javascript - 检查用户是否已在客户端登录

javascript - 如何创建一个将单行颜色从黑色变为灰色再变为白色的函数?

javascript - MySql 绑定(bind)参数不起作用

javascript - 检查益智游戏中的获胜者

javascript - 如何将暂停和播放按钮添加到 wowslider js 文件

javascript - Fancybox回调-点击函数

php - 检查用户名是否存在,然后我要更新

html - Flexbox 文本环绕