[英]disable a button if a text input is not an integer with ng-disabled
I have 2 text fields. 我有2个文本字段。
input type="text" placeholder="name" ng-model="name">
input type="text" placeholder="number" ng-model="number">
I validate that the button is not active if no data in text fields. 如果文本字段中没有数据,我将验证该按钮是否处于活动状态。 I do not know how to make the button is disabled if there is no positive integer in the text field "number". 如果文本字段“ number”中没有正整数,我不知道如何禁用按钮。
<button ng-disabled="!name || !number" value="buttonTest"></button>
I need to be a strictly input type = 'text' 我需要严格输入type ='text'
HTML: HTML:
<body ng-app="app">
<div ng-controller="myController">
<input type="text" placeholder="name" ng-model="name">
<br/>
<input type="text" placeholder="1234" ng-model="number">
<br/>
<button ng-model="button" ng-disabled="!name || !parseInt(number) || number < 0">Button</button>
</div>
</body>
Script: 脚本:
var app = angular.module('app', []);
app.controller('myController', ['$scope', function($scope) {
$scope.parseInt = parseInt;
}]);
http://plnkr.co/edit/WBVMKWYSNBAxmQdg7i2K?p=preview http://plnkr.co/edit/WBVMKWYSNBAxmQdg7i2K?p=preview
Write a function in your controller to check these values and return the result. 在控制器中编写一个函数以检查这些值并返回结果。 I sometimes use the length
property of strings to determine if there's a value. 有时,我使用字符串的length
属性来确定是否存在一个值。 It covers null, undefined, and empty strings. 它包含空,未定义和空字符串。
$scope.isPositiveInteger = function(value) {
var floatValue = parseFloat(value);
var isInteger = floatValue % 1 === 0;
return isInteger && floatValue > 0;
}
HTML: HTML:
<button ng-disabled="name.length < 1 || !isPositiveInteger(number)" value="buttonTest"></button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.