gpt4 book ai didi

javascript - Angularjs 复选框组最小和最大验证限制

转载 作者:行者123 更新时间:2023-11-29 14:47:58 24 4
gpt4 key购买 nike

我需要使用 Angular 验证(限制最小和最大选择)复选框组。 是否有任何指令可以在 Angular 上实现此验证。

<html ng-app="app">
<head>
<title>AngularJS Routing</title>
<script src="http://code.angularjs.org/1.2.7/angular.js"></script>
<script type="text/javascript">
var app = angular.module('app', []);

app.controller('CHKctrl', function ($scope) {
$scope.data = [{ "name": "CHK 1" },
{ "name": "CHK 2" },
{ "name": "CHK 3" },
{ "name": "CHK 4" },
{ "name": "CHK 5" },
{ "name": "CHK 6" }];
});
</script>
</head>
<body ng-controller="CHKctrl">
<form name="form">
<label ng-repeat="d in data">
<input
type="checkbox"
ng-model="d.value"
minlength="2"
maxlength="4"/>
{{d["name"]}} &nbsp; &nbsp;
</label>
<br />
<br />
{{data}}
</form>
</body>
</html>

最佳答案

是的,有;

<input
ng-model="string"
[name="string"]
[required="string"]
[ng-required="boolean"]
[ng-minlength="number"]
[ng-maxlength="number"]
[ng-pattern="string"]
[ng-change="string"]
[ng-trim="boolean"]>
...
</input>

您可以检查输入;

<tt>myForm.$error.minlength = {{!!myForm.$error.minlength}}</tt><br/>
<tt>myForm.$error.maxlength = {{!!myForm.$error.maxlength}}</tt><br/>

您在 HERE 进行检查.

对于复选框添加这些并根据您的要求进行改进,这是适合您的道路;

<input ng-disabled="(count>max) && (!d.check)" type="checkbox" ng-checked="d.check" ng-model="d.value" minlength="2" maxlength="4" ng.click="countCheck(d)"/>

Js:

$scope.count = 0;
$scope.max = 3;
//You can swap it with ".watch"
yourController.countCheck = function(d){
if(d.check){
$scope.count++;
}else{
$scope.count--;
}
}

关于javascript - Angularjs 复选框组最小和最大验证限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30166210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com