作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一系列行,每行一个复选框。每行都有几个输入。我已经进行了验证,要求在所有行中至少选中一个复选框。但是,到目前为止,我无法要求输入的值仅用于选中的复选框。
HTML:
<div style="width: 100%;">
<checkboxgroup-Wheelbase min-required="1">
<table style="width: 100%">
<tr>
<td>
<table style="width: 97%;">
<tr>
<th style="width: 220px;" class="wheelbaseHeaderCell">Wheelbase<br /><span style="font-weight: 400;">(choose min of 1)</span></th>
<th class="wheelbaseHeaderCell">Payload<br />[ pounds ]</th>
<th class="wheelbaseHeaderCell">Length<br />[ inches ]</th>
<th class="wheelbaseHeaderCell">Height<br />[ inches ]</th>
<th class="wheelbaseHeaderCell">Weight<br />[ pounds ]</th>
<th class="wheelbaseHeaderCell">Turn Radius<br />[ feet ]</th>
</tr>
</table>
</td>
</tr>
<tr>
<td style="height: 5px;"></td>
</tr>
<tr>
<td>
<div style="height: 170px; overflow: auto;">
<table style="width: 100%;">
<tr class="rowGroup" ng-repeat="wheelbase in wheelbases" data-rowParent="wheelbase[wheelbase.Id]">
<td class="wheelbaseCell" style="padding-left: 10px;" id="{{wheelbase.Id}}">
<!--Wheelbase-->
<label class="checkbox" for="{{wheelbase.Id}}" style="text-align: left; width: 200px;">
{{wheelbase.WheelbaseGrade}} - {{wheelbase.Inches}} inches
<input ng-model="wheelbase.checked" id="wheelbase{{wheelbase.Id}}" type="checkbox" /></label>
</td>
<td >
<!--Payload Capacity-->
<span style="display: inline-block;" ng-controller="PayloadCapacitiesCtrl">
<input ng-model="payloadCapacity.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked" />
</span>
</td>
<td >
<!--Length-->
<span style="display: inline-block;" ng-controller="VehicleLengthCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Height-->
<span style="display: inline-block;" ng-controller="VehicleHeightCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Weight-->
<span style="display: inline-block;" ng-controller="VehicleWeightCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
<td >
<!--Turning Radii -->
<span style="display: inline-block;" ng-controller="TurningRadiiCtrl">
<input data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-class="getClass({{wheelbase.Id}})" ng-disabled="!wheelbase.checked"/>
</span>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</checkboxgroup-Wheelbase>
<span class="hint" style="margin: 0 0 0 -35px; text-align: center; width: 100%;">
<button style="padding-top: 5px; ;" class="addNew" ng-click="openDialog()"><i class="icon-plus"></i> [ add new wheelbase ]</button>
</span>
.directive('checkboxgroupWheelbase', function() {
return {
restrict: 'E',
controller: function($scope, $attrs) {
var self = this;
var ngModels = [];
var minRequired;
self.validate = function() {
var checkedCount = 0;
angular.forEach(ngModels, function(ngModel) {
if (ngModel.$modelValue) {
checkedCount++;
}
});
console.log('minRequired', minRequired);
console.log('checkedCount', checkedCount);
var minRequiredValidity = checkedCount >= minRequired;
angular.forEach(ngModels, function(ngModel) {
ngModel.$setValidity('checkboxgroupWheelbase-minRequired', minRequiredValidity, self);
});
};
self.register = function(ngModel) {
ngModels.push(ngModel);
};
self.deregister = function(ngModel) {
var index = this.ngModels.indexOf(ngModel);
if (index != -1) {
this.ngModels.splice(index, 1);
}
};
$scope.$watch($attrs.minRequired, function(value) {
minRequired = parseInt(value, 10);
self.validate();
});
}
};
})
**"ng-disabled="!wheelbase.checked"**
最佳答案
您可以使用 ng-required 来完成此操作:
ng-required="wheelbase.checked"
这是更新后的 HTML 的相关部分:
<table style="width: 100%;">
<tr class="rowGroup" ng-repeat="wheelbase in wheelbases" data-rowParent="wheelbase[wheelbase.Id]" style="padding-top: 10px;">
<td class="wheelbaseCell" style="padding-left: 10px;" id="{{wheelbase.Id}}">
<!--Wheelbase-->
<label class="checkbox" for="{{wheelbase.Id}}" style="text-align: left; width: 200px;">
{{wheelbase.WheelbaseGrade}} - {{wheelbase.Inches}} inches
<input ng-model="wheelbase.checked" id="wheelbase{{wheelbase.Id}}" type="checkbox" /></label>
</td>
<td >
<!--Payload Capacity-->
<span style="display: inline-block;" ng-controller="PayloadCapacitiesCtrl">
<input ng-model="payloadCapacity.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" id="payload{{wheelbase.Id}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Length-->
<span style="display: inline-block;" ng-controller="VehicleLengthCtrl">
<input ng-model="vehicleLength.Inches" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Height-->
<span style="display: inline-block;" ng-controller="VehicleHeightCtrl">
<input ng-model="vehicleHeight.Inches" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Weight-->
<span style="display: inline-block;" ng-controller="VehicleWeightCtrl">
<input ng-model="vehicleWeight.Pounds" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
<td >
<!--Turning Radii -->
<span style="display: inline-block;" ng-controller="TurningRadiiCtrl">
<input ng-model="turningRadius.Feet" data-rowChild="{{wheelbase[wheelbase.Id]}}" type="number" style="width: 80px;" min="0" ng-disabled="!wheelbase.checked" ng-required="wheelbase.checked" ng-class="{'formRequire' : wheelbase.checked }" />
</span>
</td>
</tr>
</table>
关于angularjs - 如果选中复选框,则需要输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815463/
我是一名优秀的程序员,十分优秀!