防止隱藏表單字段在AngularJS中被驗證的最佳方式是什麼?AngularJS:防止隱藏表單字段被驗證
回答
我最初錯過了內置ngRequired
指令。還有一個required
標籤,讓我困惑。
現在,我們可以使用相同的邏輯(我們用來隱藏元素)將ngRequired
設置爲false。
下面是一個實際使用案例:我想問已婚的人他們有多少孩子,但是,如果他們沒有結婚,只是隱藏關於孩子的領域。
<form ng-app name="form">
Marital status:
<select ng-model="maritalStatus" required>
<option value="">Select...</option>
<option value="M">Married</option>
<option value="UM">Unmarried</option>
</select>
<div ng-show="maritalStatus == 'M'">
Number of children: <input type="number" ng-model="children" ng-required="maritalStatus == 'M'">
</div>
(for testing) Is this form correctly filled? {{form.$valid}}
</form>
您可以通過使用指令刪除required
屬性:
<div ng-app="myApp">
<input type="backbutton" id="firstName" name="firstName" type="text" required/>
var app = angular.module('myApp',[]);
app.directive('input',function($compile){
return {
restrict:'E',
compile:function($tElement,$tAttrs){
console.log("hi there");
var el = $tElement[0];
if(el.getAttribute('type')){
el.removeAttribute('type');
el.setAttribute($tAttrs.type,'');
return function(scope){
$compile(el)(scope);
}
}
}
}
});
app.directive('remove',function($compile){
return {
restrict: 'A',
replace:true,
template:'',
link: function (scope, element, attrs) {
element.removeAttr('required');
}
}
});
前:
<input id="firstName" name="firstName" required="" remove="" class="ng-scope">
後:
<input id="firstName" name="firstName" remove="" class="ng-scope">
Fidlle什麼也沒做!? – IamStalker
它從'input'中刪除'required'。你可以調試它,並檢查在開始時你有「必需」。 –
ahhh好的抱歉沒有注意到它。 – IamStalker
您也可以使用ng-if而不是ng-show將其從DOM /表格中完全添加或刪除。
<div ng-show="maritalStatus === 'M'">
Number of children: <input type="number" ng-model="children" ng-required="maritalStatus == 'M'">
</div>
這個
<div ng-if="maritalStatus === 'M'">
Number of children: <input type="number" ng-model="children" ng-required="true">
</div>
- 1. FlaskWTF驗證器可以防止隱藏表單字段
- 2. 隱藏字段驗證
- 3. 不驗證隱藏字段
- 4. 驗證隱藏字段
- 5. JQuery驗證:驗證隱藏字段
- 6. 使用隱藏字段驗證表格
- 7. 防止隱藏字段的變更
- 8. 在AngularJs中驗證後隱藏表格
- 9. ExtJS防止驗證帶顯示的表單字段:無
- 10. 用CodeIgniter表單驗證類重新填充隱藏字段
- 11. 支柱逃脫表單驗證的隱藏字段
- 12. Symfony2的額外領域FormError與隱藏字段驗證表單
- 13. 與隱藏字段一起進行表單驗證
- 14. symfony表單驗證郵件隱藏字段未顯示
- 15. 如何防止隱藏字段干擾MVC中的服務器端驗證?
- 16. 防止Vim隱藏字符
- 17. 隱藏字段的Coldfusion cfform驗證
- 18. 隱藏字段上的Knockout.js驗證
- 19. 隱藏字段的必需驗證
- 20. 驗證隱藏的輸入字段
- 21. 需要隱藏字段驗證mvc
- 22. MVC3隱藏字段的驗證
- 23. 必填字段驗證不隱藏
- 24. C#沒有驗證時隱藏字段
- 25. 的隱藏字段執行驗證
- 26. symfony隱藏字段,但驗證它們
- 27. jQuery:驗證首先隱藏的字段
- 28. 避免驗證隱藏字段 - Webmatrix
- 29. 如何驗證隱藏字段
- 30. 驗證碼和隱藏字段
等都可能只是刪除了'required'屬性,當你隱藏了它? – Cherniv
如果你有代碼,請發佈它來幫助我們,有足夠的選項來實現你的邏輯,但是不要從頭開始,而只是改變你的代碼。小提琴/ PLUNKER會很棒 –
有沒有人回答你的問題?許多人只是給你選擇禁用所需的驗證,但沒有任何其他......像,數字......或者我錯過了什麼? – genuinefafa