我选择了两个值时启用按钮

时间:2014-11-13 11:11:35

标签: angularjs

我有两个选择我想在我的两个选择中选择值时启用我的按钮,我不知道我该怎么做?

JSFIDDLE

我的HTML:

<div ng-app="myapp">
    <fieldset ng-controller="FirstCtrl">
        <select 
            ng-options="people.first for people in people_1"
            ng-model="selectedPerson1"></select>
        <select
               ng-options="people.first for people in people_2"
            ng-model="selectedPerson2"></select>
    </fieldset>
    <br/><button class="btn btn-primary" ng-click="platform=true">Comparer</button>
</div>

2 个答案:

答案 0 :(得分:1)

您可以使用ng-disabled指令执行此操作。我在这里修改了你的jsfiddle:http://jsfiddle.net/p0t4cw84/3/。我将按钮移动到控制器FirstCtrl的范围内,并在范围上添加了一个方法,如果选择了值,它将返回true或false。

$scope.enableCompare = function () {
    return !($scope.selectedPerson1 && $scope.selectedPerson2);
};

我希望这会有所帮助。

答案 1 :(得分:1)

你走了。你小提问了很多问题。我修好了。

http://jsfiddle.net/purpz7u3/

<div ng-app="myapp">
    <div ng-controller="FirstCtrl">
    <fieldset >
        <select 
            ng-options="people.first for people in people_1"
            ng-model="selectedPerson1" ng-change="check()"></select>
        <select
               ng-options="people.first for people in people_2"
            ng-model="selectedPerson2" ng-change="check()"></select>
    </fieldset>
                <br/><button class="btn btn-primary" ng-disabled="checked">Comparer</button>
    </div>

</div>

var myapp = angular.module('myapp',[]); myapp.controller('FirstCtrl',function($ scope){     $ scope.checked = true;

$scope.people_1 = [
    { id: 1, first: 'John' },
    { id: 2, first: 'Rocky' }
];
   $scope.people_2 = [
    { id: 1, first: 'Rambo' },
    { id: 2, first: 'Balboa'}
];
$scope.check = function(){
    if($scope.selectedPerson1 && $scope.selectedPerson2){
        $scope.checked=false;
    }
}

});