使用form.submit验证复选框部分
Using form.submit to validate checkbox section
我有一个名称传递给控制器的表单,当表单为$valid时,我将继续处理数据。这曾经适用于单选按钮,但当我切换到复选框时,只有在选中所有复选框时才能通过验证。如何做到这一点,选择1将使表单有效?供参考的代码片段:
<form name="snippetForm" ng-submit="vm.submitForm(snippetForm)" novalidate>
<input type="checkbox" ng-model="vm.destination.1" ng-true-value="'1'" name="location" required >
<input type="checkbox" ng-model="vm.destination.2" ng-true-value="'2'" name="location" required >
<input type="checkbox" ng-model="vm.destination.3" ng-true-value="'3'" name="location" required >
</form>
vm.submitForm = function(form) {
if(form.$valid) {
console.log('Passes only when all checkboxes from form.location are selected')
}
}
我在每个复选框上添加了一个ng-change
表达式。如果未选中任何复选框,表达式将使表单无效。
<form name="snippetForm" ng-submit="vm.submitForm(snippetForm)" novalidate>
<input type="checkbox" ng-model="vm.destination1" ng-true-value="'1'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
<input type="checkbox" ng-model="vm.destination2" ng-true-value="'2'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
<input type="checkbox" ng-model="vm.destination3" ng-true-value="'3'" name="location" ng-change="{{(!vm.destination1 && !vm.destination2 && !vm.destination3) ? (snippetForm.$valid = false) : (snippetForm.$valid = snippetForm.$valid)}}" >
</form>
相关文章:
- 正在验证8个真/假复选框或复选框中的2个
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- angularjs复选框验证至少两个
- 聚合物铁形式验证至少选中一个复选框
- HTML5复选框组验证
- Javascript验证(gen_validatorv4.js)-基于复选框结果
- jquery表单验证复选框错误显示
- 切换复选框和验证的类
- 如何在选中“我的复选框”后验证我的表单字段
- 如何使用javascript asp.net在网格视图的同一行选中复选框时验证网格视图中的文本框
- 使用Javascript的ASP.NET复选框列表验证
- 如何使用单选和复选框按钮验证javascript验证
- 当输入name[]时,JS对复选框的验证不起作用
- 根据复选框条件验证输入文本字段(max和min)
- MVC 3复选框关闭验证而不使用requireattribute
- 如何在复选框中验证多个名称的复选框(BOOTSTRAP VALIDATOR USED)
- 复选框检查验证
- 几个复选框和验证jQuery
- 如何在循环中有复选框的情况下对多个复选框应用验证
- 如何在RadGrid中使用JavaScript验证复选框时验证文本框