document.getElementById("")。value返回对象而不是值

时间:2017-01-18 08:19:37

标签: javascript angularjs angularjs-ng-options

我有以下给定格式的数据

{
 USA: { 
    CA: { 'SAN FRANCISCO':['94188', '94158', '94143'] },
    LA: { 'BATON ROUGE':['70898','70895','70891'] }
  }
} 

尝试将它们添加到依赖的下拉列表中,即Country,State,City和Zip代码。我使用ng-option但是在获取国家/地区下拉列表的值时返回的对象:16而不是值。

$scope.countries = {
    USA: { 
       CA: { 'SAN FRANCISCO':['94188', '94158', '94143'] },
       LA: { 'BATON ROUGE':['70898','70895','70891'] }
    }
}

$scope.GetSelectedCountry = function () {
    $scope.strCountry = document.getElementById("country").value;
}  

<b>Country:</b>
&nbsp;
<select id="country" ng-model="statessource" ng-disabled="!type" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
<option value=''>Select Country</option> 

4 个答案:

答案 0 :(得分:3)

获取值不是 Angular方式

您使用<select ng-model="statessource" ...>,因此select的值存储在$scope.statessource

这应该有效:

$scope.GetSelectedCountry = function() {
    $scope.strCountry = $scope.statessource;
}  

答案 1 :(得分:1)

您已为选择标记使用了ng-model变量,但尚未在控制器中使用该变量来获取所选值。无需使用javascript dom选择器访问该值,而是可以直接使用select标记$scope.statessource的模型值。

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            $scope.countries = {
                USA: {
                    CA: {
                        'SAN FRANCISCO': ['94188', '94158', '94143']
                    },
                    LA: {
                        'BATON ROUGE': ['70898', '70895', '70891']
                    }
                }
            }

            $scope.GetSelectedCountry = function () {
                $scope.strCountry = $scope.statessource;
                console.log($scope.strCountry);
            }
        });
    </script>
</head>

<body ng-app="myApp" ng-controller="myCtrl">

    <b>Country:</b> &nbsp;
    <select id="country" ng-model="statessource" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
        <option value=''>Select Country</option>
    </select>

</body>

</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果你唯一关心的是每次用户更改时都获得当前选择的值,那么ng-model就已经处理了这种情况。 所以,通过使用这个

<select id="country" ng-model="statessource" ng-disabled="!type" ng-options="country for (country, states) in countries">

Angular将始终应用其双向数据绑定将当前选定的选项存储在$scope.statessource - 变量中。

除此之外,如果您想要做与更改选择相关的其他内容,您可以执行以下操作:

<select id="country" ng-model="statessource" ng-disabled="!type" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry(statessource)">

$scope.GetSelectedCountry = function (newState) {
  // Do something with your newState variable.
  // Remember, $scope.statessource still includes the currently selected
  // option
}

我希望有所帮助:)

答案 3 :(得分:-2)

访问嵌套在(:Person {forename:John, surname:Smith})-[:HAS_NUMBER]->(:Number {compNumber:001}) (:Person {forename:John, surname:Smith})-[:HAS_NUMBER]->(:Number {compNumber:002}) (:Person {forename:John, surname:Smith})-[:HAS_NUMBER]->(:Number {compNumber:003}) 中的OPTION的选定值的方法如下:

SELECT