我有一个表,其中的数据来自某些json。在这里,我需要根据特定行对输入字段进行验证。如果用户可以填写特定行的至少一个文本框,则用户可以提交表单。如果第一行的任何一个字段包含一些文本,则用户可以提交。但是如果第一行的所有文本框为空,则用户无法提交。第二行/第三行也是如此。下面是我的代码。 >
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css'>
</head>
<body>
<div class="container" ng-app="problemApp" data-ng-controller="validationCtrl">
<form name="form1">
<table>
<tr ng-repeat="item in testdata">
<td>
<input type="text" required value={{item.name}}>
</td>
<td>
<input required type="text" >
</td>
<td>
<input required type="text" >
</td>
</tr>
</table>
<button type="submit">submit</button>
</form>
</div>
<script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js'></script>
<script src="js/index.js"></script>
var app=angular.module('problemApp', []);
app.controller('validationCtrl',function($scope,$http){
var request = {
method: 'get',
url: 'test.json',
dataType: 'json',
contentType: "application/json"
};
$scope.testdata = new Array;
$http(request)
.success(function (json) {
$scope.testdata = json;
$scope.list = $scope.testdata;
})
.error(function () {
});
$scope.dataTableOpt = {
//custom datatable options
// or load data through ajax call also
"aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
};
});
[{
"countryId": 1,
"name": "France - Mainland",
"desc": "some description"
},
{
"countryId": 2,
"name": "Gibraltar",
"desc": "some description"
},
{
"countryId": 3,
"name": "Malta",
"desc": "some description"
}
]
答案 0 :(得分:0)
ng-submit
指令以提交表单。 https://docs.angularjs.org/api/ng/directive/ngSubmit 与您的要求有关的所有逻辑将在此函数中,您可以在ng-submit
上调用此函数。可能是这样的:
let showError = flase;
$scope.formSubmit = function(){
$scope.testdata.map((item)=>{
if(item == ''){
showError = true; // Using this variable, you show the error in your view.
}
});
}
此formSubmit
函数将在您的HTML中用作:<form name="form1" ng-submit="formSubmit()">
最后,您应多次删除HTML required
属性,以允许填充任何一个(而不是全部)。
希望它对您有帮助。