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

javascript - Plus minus with single textbox in angularjs - Stack Overflow

programmeradmin3浏览0评论

I have two buttons and one text box I want, quantities to be increamented by 1 on click of +plus button and decremented by 1 on click of -minus button. But when it reaches to less than 0 or say 0 the decrement button should be disabled. I have this code here PLunker

My html code

<form id='myform' >
 <button ng-click="increment()"  class='qtyminus'>-</button>
    <input ng-modal="item.qty" type='text' name='quantity' value='0' class='qty' />
    <button ng-click="decrement()" >+</button>
</form>

My js code

 $scope.increment = function () {
        alert('hi');
        $scope.item.qty++;
    };
    $scope.decrement = function () {
           $scope.item.qty--;
    };

I have two buttons and one text box I want, quantities to be increamented by 1 on click of +plus button and decremented by 1 on click of -minus button. But when it reaches to less than 0 or say 0 the decrement button should be disabled. I have this code here PLunker

My html code

<form id='myform' >
 <button ng-click="increment()"  class='qtyminus'>-</button>
    <input ng-modal="item.qty" type='text' name='quantity' value='0' class='qty' />
    <button ng-click="decrement()" >+</button>
</form>

My js code

 $scope.increment = function () {
        alert('hi');
        $scope.item.qty++;
    };
    $scope.decrement = function () {
           $scope.item.qty--;
    };
Share asked Aug 16, 2016 at 9:23 Sudarshan KalebereSudarshan Kalebere 3,9373 gold badges35 silver badges66 bronze badges 1
  • are you sure that you've provided the right code in Plunker? When I click "+" for incrementing I get a "Bad request" error. And If I press "-" it alerts hi and throws the same error after that. However your question should be solved with the answer given here: stackoverflow./questions/30505010/… – Megajin Commented Aug 16, 2016 at 9:28
Add a ment  | 

2 Answers 2

Reset to default 4

Use this code. This should solve your problem

<form id='myform' >
 <button ng-click="increment()"  class='qtyminus'>-</button>
    <input ng-model="item.qty" type='text' name='quantity' value='0' class='qty' />
    <button ng-click="decrement()" ng-disabled="item.qty <= 0">+</button>
</form>
  • Use ng-diabled directive
  • Correct ty typo @ ng-modal, it is ng-model

Note: Consider <=0 condition as user may enter <0 value in input-element

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.qty = 10;
  $scope.increment = function() {
    $scope.qty++;
  };
  $scope.decrement = function() {
    $scope.qty--;
  };
});
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form id='myform' ng-app="myApp" ng-controller="myCtrl">
  <button ng-click="decrement()" class='qtyminus' ng-disabled="qty<=0">-</button>
  <input ng-model="qty" type='text' name='quantity' class='qty' />
  <button ng-click="increment()">+</button>
</form>

发布评论

评论列表(0)

  1. 暂无评论