我有一個帶有文本框和複選框的窗體。當我填充值到複選框,值被推入到數組中。當我取消選中複選框時,該值將從該數組中刪除。 這是我的工作code pen.跟蹤數組中的已刪除索引javascript
這是我的表單代碼
<label class="item item-input">
<input type="text" placeholder="In a word, what is important to you?" ng-model="values.first" >
<ion-checkbox ng-click="toggleCheckBox(success.first,values.first)" ng-change="show2='true'" ng-model="success.first"
ng-disabled="!values.first" style="border: none;padding-left: 30px;" class="checkbox-royal"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show2" ng-class="{'animated-custom slideInLeft':success.first}">
<input type="text" placeholder="What else is important to you?" ng-model="values.second" >
<ion-checkbox class="checkbox-royal" ng-model="success.second" ng-click="toggleCheckBox(success.second,values.second)" ng-change="show3='true'" ng-disabled="!values.second" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show3" ng-class="{'animated-custom slideInLeft':success.second}">
<input type="text" placeholder="What else is important to you?" ng-model="values.third">
<ion-checkbox class="checkbox-royal" ng-model="success.third" ng-click="toggleCheckBox(success.third,values.third)" ng-change="show4='true'" ng-disabled="!values.third" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
<label class="item item-input" ng-show="show4" ng-class="{'animated-custom slideInLeft':success.third}">
<input type="text" placeholder="What else is important to you?" ng-model="values.four">
<ion-checkbox class="checkbox-royal" ng-model="success.four" ng-click="toggleCheckBox(success.four,values.four)" ng-change="show5='true'" ng-disabled="!values.four" style="border: none;padding-left: 30px;"></ion-checkbox>
</label>
這是控制器代碼,
.controller('myctrl',function($scope){
var values=[];
$scope.toggleCheckBox=function(isChecked, value){
if(isChecked){
values.push(value);
console.log(values);
}else{
var currentIndex = values.indexOf(value);
values.splice(currentIndex, 1);
console.log(values);
//console.log(values.indexOf(value));
}
}
})
的問題是,當我從數組中刪除的價值,並再次檢查複選框,它將值推到最後。是否有可能在數組中保留該值的原始位置?
文本框控件的數量是否固定爲最大值4或超出最終用戶的期望? – gkb