1. 程式人生 > >AngularJS判斷checkbox/複選框是否選中並實時顯示

AngularJS判斷checkbox/複選框是否選中並實時顯示

最近因為專案原因重新撿起來了AngularJS ,遇到老問題複選框選中和值的問題。
先貼以前網上找的解決方案
http://www.cnblogs.com/CheeseZH/p/4517701.html
個人感覺太麻煩了,程式碼太多,然後自己找了點資料,現在如下自己的解決方案
全選checkbox按鈕

<input type="checkbox" ng-model="all"> <span>全選</span>
ng-repeat裡面的checkbox按鈕
<form name="testFrom"  action="">
     <div  ng-repeat="x in shoppingCartList" >
		<input type="checkbox" ng-checked="all" value="{{x.userId}}" name="userId" "></div>
        </div>
</form name="testFrom">
獲取值的方法 
$scope.deleteUserProduct = function(){
	$scope.userIdList= '';                           //先賦值 '' 避免append的時候 undefined
	var formEle = document.testFrom.elements.userId; //獲取form表單name等於userId元素
	for (var i = 0; i < formEle.length; i++) {       //遍歷userId元素
		if(foemEle[i].checked){  //判斷是否選中
			$scope.userIdList +=formEle[i].value+",";
		}
	}
}
java後臺程式碼
String [] shopId=request.getUserIdList().split(",");
短短的幾行Js程式碼解決了.如果你有更好的解決方案,可以底下留言。一起探討。哈哈