简体   繁体   English

使用angularjs提交后如何获取选中的复选框值?

[英]How to get checked checkbox value after submit using angularjs?

I have list of checkbox with some offer when I submit have to get checked checkbox values .I have tried but its not working I have added my code below.now I got offer message id but I didt get checked checkbox values after submit pls help me out. 我有一些要约的复选框列表,当我提交必须获得选中的复选框值时。我已经尝试过,但无法正常工作,我在下面添加了我的代码。现在我收到了要约消息ID,但是在提交请帮助后我没有得到选中的复选框值出。 demo 演示

 function Test1Controller($scope) { var storeid = window.localStorage.getItem("storeid"); var serverData = ["Samsung Galaxy Note", "Samsung Galaxy S6", "Samsung Galaxy Avant","Samsung Galaxy Young"]; $scope.items= [] ; $scope.selection = []; $scope.offers = [{ id: "as23456", Store: "samsung", Offer_message:"1500rs off", modalname: "Samsung Galaxy Young" },{ id: "de34575", Store: "samsung", Offer_message:"20% Flat on Samsung Galaxy S6", modalname: "Samsung Galaxy S6" },] $scope.toggleSelection = function toggleSelection(item) { $scope.gotOffers=[]; var idx = $scope.selection.indexOf(item); // is currently selected if (idx > -1) { $scope.selection.splice(idx, 1); } // is newly selected else { $scope.selection.push(item); } for(var i=0;i<$scope.selection.length;i++){ for(var j=0;j<$scope.offers.length;j++){ console.log($scope.selection[i].name +" "+ $scope.offers[j].modalname) if( $scope.selection[i].name == $scope.offers[j].modalname){ var idx = $scope.gotOffers.indexOf($scope.offers[j].Offer_message); if(idx == -1){ console.log("inside idx") $scope.gotOffers.push($scope.offers[j]); } } } } console.log($scope.offers); }; var selectedvalue = window.localStorage.getItem("selectedvalue"); // here selected value Samsung Galaxy S6 var selectedvalue="Samsung Galaxy S6,Samsung Galaxy Young"; for(var i=0;i<serverData.length;i++){ var modal = { name:serverData[i], selected:false }; if (selectedvalue.indexOf(serverData[i]) >= 0 || null){ $scope.toggleSelection(modal); } $scope.items.push(modal); } //----------------------------Our Shop Offers---------------------------------------- $scope.check = function(){ var checkedItems = []; console.log($scope.offerSelected) for(var i=0;i<$scope.items.length;i++){ if($scope.items[i].selected){ checkedItems.push($scope.items[i].name); } } console.log(checkedItems) ; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script> <div ng-app> <div ng-controller="Test1Controller" > <div ng-repeat="item in items"> <input type="checkbox" ng-checked="selection.indexOf(item) >= 0" ng-click="toggleSelection(item)"/> {{item.name}} </div> <select ng-show="gotOffers.length > 0" ng-model="offerSelected"> <option ng-repeat="offer in gotOffers" value="{{offer.id}}">{{offer.Offer_message}}</option> </select> <input type="button" name="submit" value="submit" ng-click="check()"/> </div> </div> 

It seems like you are wanting to iterate over $scope.selection rather than $scope.items . 似乎您想遍历$scope.selection而不是$scope.items

Updated Example 更新示例

Change: 更改:

var checkedItems = [];$scope.selection
for (var i = 0; i < $scope.items.length; i++) {
  if ($scope.items[i].selected) {
    checkedItems.push($scope.items[i].name);
  }
}

to: 至:

var checkedItems = [];
for (var i = 0; i < $scope.selection.length; i++) {
  checkedItems.push($scope.selection[i].name);
}

The if conditional statement was removed since all the elements in $scope.selection are already checked. 由于已检查$scope.selection中的所有元素,因此删除了if条件语句。


Alternatively, you could also just use the .map() method to create the array: 另外,您也可以只使用.map()方法创建数组:

Updated Example 更新示例

var checkedItems = $scope.selection.map(function(checkbox) {
  return checkbox.name;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM