在我的HTML表单中有5个复选框。如何在提交表格时检查选中的复选框?提交表单时获取角度js中动态复选框的值
0
A
回答
0
您可以使用Angular.js的Checklist-model。这是非常有用:
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl1', function($scope) {
$scope.roles = [
'guest',
'user',
'customer',
'admin'
];
$scope.user = {
roles: ['user']
};
$scope.checkAll = function() {
$scope.user.roles = angular.copy($scope.roles);
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push('guest');
};
$scope.showCheckedOnes = function() {
var checkedBoxes = "";
for (var i = 0; i < $scope.user.roles.length; i++) {
checkedBoxes += $scope.user.roles[i] + " ";
}
alert(checkedBoxes);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://vitalets.github.io/checklist-model/checklist-model.js"></script>
<div ng-app="app">
<div ng-controller="Ctrl1">
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
</label>
<div>
<div>
<button ng-click="checkAll()" style="margin-right: 10px">Check all</button>
<button ng-click="uncheckAll()" style="margin-right: 10px">Uncheck all</button>
<button ng-click="checkFirst()" style="margin-right: 10px">Check first</button>
<button ng-click="showCheckedOnes()" style="margin-right: 10px">Show checked ones</button>
0
您可以定义数组值并映射到html。
$scope.checkbox = [];
HTML
<input ng-model="checkbox[0] type="checkbox" />
<input ng-model="checkbox[1] type="checkbox" />
<input ng-model="checkbox[2] type="checkbox" />
<input ng-model="checkbox[4] type="checkbox" />
,也可以定义为复选框对象,变化选择框[指标]到复选框[名]。
1
我用这一小段代码。随意采取。
在你的控制器:
$scope.sentinel = [];
$scope.toggleSelection = function(value) {
// Bit trick, equivalent to "contains"
if (~$scope.sentinel.indexOf(value)) {
var idx = $scope.sentinel.indexOf(value);
$scope.sentinel.splice(idx, 1);
return;
}
$scope.sentinel.push(value);
};
然后在你的HTML:
<span ng-repeat="key in $scope.yourarray">
<md-checkbox style="margin-left:30px;" aria-label="Select item"
ng-click="toggleSelection(key)"><span>{{ key }}</span></md-checkbox>
<br/>
</span>
这可以让你有任何大小的阵列,通过sentinel
阵列已经登记检查的值。如果再次检查一个框,toogleSelection
将阻止您再次添加值。
相关问题
- 1. 获取角度复选框
- 2. 获取角度js中的选定值
- 3. 春季提交表单获取复选框值不工作
- 4. 如何获取在单击提交角度js后单选按钮中选择的值列表
- 5. 获取角度手表中的复选框组值
- 6. 从表单提交中取出mysql的post复选框值
- 7. 如何获得角度js中的复选框值?
- 8. 如何动态创建复选框并获取所有选中的复选框的角度值2
- 9. 如何在提交表单时获取未勾选的复选框列表?
- 10. 获取表单上的复选框提交并将值插入到textarea中
- 11. 无法读取复选框数组的值提交表单
- 12. 为什么复选框会在我提交表单时自动取消选中?
- 13. 从复选框jQuery获取动态值
- 14. 角度从复选框中获取所选值
- 15. 复选框验证为“开”时,提交表单时选中
- 16. 获取复选框数组值在角JS中
- 17. 角度动态多重复选框过滤器JS
- 18. 提交表单时获取选择列表的值 - Drupal
- 19. 从角度2打字稿中的复选框获取值
- 20. 如何以角度2动态获取单选按钮值?
- 21. 检索动态复选框的值,并传递给提交值
- 22. 点击提交表单的复选框
- 23. 如果取消选中复选框,则禁用表单提交
- 24. 使用Angular JS进行表单提交的复选框验证
- 25. 获取选择框的值上提交
- 26. 角度:表单提交与动态下拉列表
- 27. 无法重置复选框以取消选中角度js
- 28. jQuery:当选中复选框时提交表单
- 29. 当复选框被选中时提交表单
- 30. 当复选框被选中时提交表单 - 教程
感谢您花费宝贵的时间来解决我的问题。 –
不客气 – webmaster