gpt4 book ai didi

javascript - 如果在 ng-repeat 中未填写字段,如何禁用按钮?

转载 作者:行者123 更新时间:2023-11-30 17:09:45 24 4
gpt4 key购买 nike

大家好 我在保存按钮方面遇到了一些问题。如果没有在任何索引中输入数据,我禁用我的按钮。如果所有 index 都是空白,那么 my button should be disabled 如果其中一个 index 被填充,那么 enable my button

这是我的 Plunkr 链接 http://plnkr.co/edit/OQxi53xtVKOgToPhzDUZ?p=preview

这是我的 html 文件:

  <!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.4
/angular.min.js">
</script>
<script src="script.js"></script>
`enter code here`</head>
<body>
<form action="#" ng-controller='detailContrller'>
<div class="control-group" ng-repeat="story in stories"> <br>
<h4> Enter Data </h4>
Name : <input type="text" data-ng-model="selection[story].Name1"
placeholder="Enter Name"> <br>
Address : <input type="text" data-ng-model="selection[story].Name2"
placeholder="Enter Address"> <br>
City : <input type="text" data-ng-model="selection[story].Name3"
placeholder="Enter City"> <br>
Phone : <input type="text" data-ng-model="selection[story].Name4"
placeholder="Enter Phone "> <br>
State : <input type="text" data-ng-model="selection[story].Name5"
placeholder="Enter State"> <br>

<input type="checkbox" ng-model="selection[story].all"
ng-change="updateAll(story)">
<label class="control-label">IncludeAll {{story}}</label>
<div class="controls">
<label class="checkbox inline" ng-repeat="browser in browsers">
<input type="checkbox" value="{{browser}}"
ng-model="selection[story].browsers[browser]"
ng-change="checkChange(browser)"
> {{browser}}
</label>
</div>
</div>
<button type="button" data-ng-click="save()">Save</button>
<pre>{{selection | json}}</pre>
</form>
</body>
</html>

控制者

var app = angular.module("myApp",[]);
app.controller('detailContrller', function($scope){

$scope.stories = [];
$scope.browsers = ['IE', 'Chrome', 'Firefox','Safari','Opera'];
$scope.selection = {};
$scope.details = {};
var checked;
$scope.updateAll = function (story) {
checked = $scope.selection[story].all;
$scope.browsers.forEach(function (browser) {
$scope.selection[story].browsers[browser] = checked;
});
};

for(var i = 0; i< 3; i++) {
$scope.stories.push(i+1);
}
$scope.checkChange = function (browser) {
for(var i =0; i< $scope.stories.length; i++){
if(!$scope.stories[i].selected){
$scope.checked = false
return false;
}
}
}
angular.forEach($scope.stories, function(storgy) {
$scope.selection[storgy] = {
browsers: {}
};
});

$scope.save = function () {
console.log($scope.selection);
}
})

请检查这个并给我一个解决方案提前致谢。

最佳答案

您可以通过将 name 添加到 form 并将“required” attribute 添加到 inputs 和 ng-disabled 到按钮:

<form action="#" ng-controller='detailContrller' name="MyForm">
<div class="control-group" ng-repeat="story in stories"> <br>
<h4> Enter Data </h4>
Name : <input type="text" data-ng-model="selection[story].Name1"
placeholder="Enter Name" required> <br>
Address : <input type="text" data-ng-model="selection[story].Name2"
placeholder="Enter Address" required> <br>
City : <input type="text" data-ng-model="selection[story].Name3"
placeholder="Enter City" required> <br>
Phone : <input type="text" data-ng-model="selection[story].Name4"
placeholder="Enter Phone " required> <br>
State : <input type="text" data-ng-model="selection[story].Name5"
placeholder="Enter State" required> <br>

<input type="checkbox" ng-model="selection[story].all" ng-change="updateAll(story)">
<label class="control-label">IncludeAll {{story}}</label>
<div class="controls">
<label class="checkbox inline" ng-repeat="browser in browsers">
<input type="checkbox" value="{{browser}}" ng-model="selection[story].browsers[browser]" ng-change="checkChange(browser)"
required > {{browser}}
</label>
</div>
</div>
<button type="button" data-ng-click="save()" ng-disabled="MyForm.$invalid">Save</button>
<pre>{{selection | json}}</pre>
</form>

http://plnkr.co/edit/qJjt5bLHbCVnHW5k2XEZ?p=preview

在此处查看验证部分:

https://docs.angularjs.org/guide/forms

关于javascript - 如果在 ng-repeat 中未填写字段,如何禁用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27273241/

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