Angularjs Angular js验证无法正常工作
我尝试在表单中使用angular js验证,但由于某些原因,它无法正常工作,请查看我的代码:Angularjs Angular js验证无法正常工作,angularjs,Angularjs,我尝试在表单中使用angular js验证,但由于某些原因,它无法正常工作,请查看我的代码: <table class="table table-hover table-bordered"> <form name="editPercentageForm" id="editPercentageForm" novalidate> <thead> <tr> <th>From Hours</
<table class="table table-hover table-bordered">
<form name="editPercentageForm" id="editPercentageForm" novalidate>
<thead>
<tr>
<th>From Hours</th>
<th>To Hours</th>
<th>Percentage</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cancellationPercentData in cancellationPercent">
<td class="form-group">
<input type="number" name="editFromHours" ng-model="cancellationPercentData.fromHours" class="form-control" ng-disabled="disableField" id="{{ $index }}fromHours" required/>
<p ng-show="editPercentageForm.editFromHours.$touched && editPercentageForm.editFromHours.$invalid">This field is required</p>
<td class="form-group">
<input type="number" name="edittoHours" ng-model="cancellationPercentData.toHours" class="form-control" ng-disabled="disableField" id="{{ $index }}toHours" required/>
<p ng-show="editPercentageForm.edittoHours.$touched && editPercentageForm.edittoHours.$invalid">This field is required</p>
<p ng-show="edittoHours>=editFromHours" class="text-danger">To Hours should not be more than to Hours</p>
</td>
<td class="form-group">
<input type="text" name="editPer" ng-model="cancellationPercentData.percentage" class="form-control" ng-disabled="disableField" id="{{ $index }}percentage" required/>
<p ng-show="editPercentageForm.editPer.$touched && editPercentageForm.editPer.$invalid">This field is required</p>
</td>
<td class="form-group">
<button class="btn btn-success col-xs-12" type="button" ng-click="disableField = false" ng-show="disableField" style="margin-bottom: 1%;">Edit</button>
<button class="btn btn-success col-xs-12" type="submit" style="margin-bottom: 1%;" ng-disabled="editPercentageForm.$invalid" ng-click="updateCancellations(cancellationPercentData, $index+'fromHours', $index+'toHours', $index+'percentage')" ng-show="disableField == false">Update</button>
<button class="btn btn-primary col-xs-12" type="button" ng-click="deleteCancellations(cancellationPercentData)" style="margin-bottom: 1%;">Delete</button>
</td>
</tr>
</tbody>
</form>
</table>
从几个小时开始
到小时
百分比
此字段是必需的
此字段是必需的
到小时不应超过到小时
此字段是必需的
编辑
更新
删除
提前感谢:)不要点击按钮提交表单。在ng提交表单标签上执行此操作。 这将不允许表单在其完全验证之前提交 此外,这种方法可能比这种方法更好。你应该每行做一张表格。 这意味着您的表单也应该在
ng repeat
中
这不会让您的表单在未经验证的情况下被提交
另一种方法是在updateCancellations()
函数的函数定义中调用validate函数。
如果自定义验证函数返回false,只需从那里返回执行,否则让它执行到完成。不要在单击按钮时提交表单。在ng提交表单标签上执行此操作。 这将不允许表单在其完全验证之前提交 此外,这种方法可能比这种方法更好。你应该每行做一张表格。 这意味着您的表单也应该在
ng repeat
中
这不会让您的表单在未经验证的情况下被提交
另一种方法是在updateCancellations()
函数的函数定义中调用validate函数。
如果自定义验证函数返回false,只需从那里返回执行,否则让它执行到完成。您必须在表单行以ng submit的形式提交表单,而不是按钮,ng repeat应该在表单级别。这将用于验证所有字段。您必须在表单行以ng submit的形式提交表单,而不是按钮,ng repeat应该在表单级别。这将适用于所有验证字段。即使将ng单击替换为ng提交,我的验证也无法正常工作。我只是更新了我的方法答案。看一看@哈里希会试试看,让你知道的。如果您无法通过,请在此处@HarishEven评论,将ng click替换为ng submit。我的验证无法正常工作。我只是更新了我的答案。看一看@哈里希会试试看,让你知道的。如果您无法通过,请在这里@HarishAny updates发表评论?你能解决吗?有更新吗?你能解决它吗?