0

I have one button called test and one drop-down menu with three values. How to enable-disable Test button according to value selected in drop down menu. Eg. If selected "Not Ready" , button should disable If selected Ready or Attention, button should enable

<div class="row">
    <div>
    <button id="testBtn" class="btn btn-default" >Test</button>
    </div>
</div>


<div class="row">
<div class="col-md-12">


    <select name="select">
        <option value="value1" selected>Not ready</option> 
        <option value="value2">Ready</option>
        <option value="value3">Attention !!</option>
    </select>


 </div>
</div>

See Plunker

3
  • And what have you tried? Your example has neither an angular app, nor a controller. Just straight-up HTML. Commented Dec 29, 2015 at 21:39
  • sorry !! New to AngularJs !! Commented Dec 29, 2015 at 21:50
  • 2
    It's ok to be new to it. But you need to actually try to do it first instead of just asking someone else to write your code for you. Commented Dec 29, 2015 at 22:25

3 Answers 3

5

You need an Angular app and controller. From there you can bind a model to your select, and then use an expression with the ng-disabled directive on the button to enable and disable it dynamically depending on the value in the box. See below. ng-model on the select binds it to $scope.currentState which is what I compare my literal string against in the ng-disabled directive on the button.

var app = angular.module('myapp', []);

app.controller('MainCtrl', function($scope) {
  $scope.states = ['Not ready', 'Ready', 'Attention !!'];
  $scope.currentState = 'Not ready';
});
<!DOCTYPE html>
<html ng-app="myapp">

<head>
  <meta charset="utf-8" />
  <title>AngularJS App</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl">
  <div class="row">
    <div>
      <button id="testBtn" class="btn btn-default" ng-disabled="currentState === 'Not ready'">Test</button>
    </div>
  </div>


  <div class="row">
    <div class="col-md-12">


      <select name="select" ng-model="currentState" ng-options="state as state for state in states">
      </select>


    </div>
  </div>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

1

If the validation value is number then use this method.

            <div class="form-group">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            <label class="control-label  col-xs-4 col-sm-4 col-md-4 col-lg-4">User Type</label>
                            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                <select class="form-control height_30" ng-model="data1.user_type " ng-options="cust.id as cust.typename for cust in usertype"></select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            <label class="control-label  col-xs-4 col-sm-4 col-md-4 col-lg-4">Password</label>
                            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                <input type="text" class="form-control" ng-model="data1.user_passwd">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="row">
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            <label class="control-label  col-xs-4 col-sm-4 col-md-4 col-lg-4">Customer</label>
                            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                                <select ng-disabled="data1.user_type === 1 || data1.user_type === 2" class="form-control height_30" ng-model="data1.customer_id" ng-options="cust.customer_id as cust.customer_name for cust in customers"></select>
                            </div>
                        </div>
                    </div>
                </div>

Comments

1
<select ng-model="item" ng-options="a.name for a in data">
    <option value="">
        select
    </option></select>
<button class="btnS btn-success ctrl-btn" ng-click="save()" ng-disabled="!item"> Submit</button>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.