2016-11-15 100 views
1

我有一个带有文本框和复选框的窗体。当我填充值到复选框,值被推入到数组中。当我取消选中复选框时,该值将从该数组中删除。 这是我的工作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)); 
    } 
    } 
}) 

的问题是,当我从数组中删除的价值,并再次检查复选框,它将值推到最后。是否有可能在数组中保留该值的原始位置?

+0

文本框控件的数量是否固定为最大值4或超出最终用户的期望? – gkb

回答

1

传入toggleCheckBox项目索引并设置要喜欢这取决于通过索引数组项的值和值:

var x = Array(); 
var index = 2; 
x[index] = 'A'; 
console.log(x); 

但要确保,当你想使用数组遍历并用undefined值拼接项目。

1

添加第三个参数toggleCheckBox与位置 - 是这样的:

... 
<ion-checkbox class="checkbox-royal" ng-model="success.four" ng-click="toggleCheckBox(success.four,values.four,4)" 
... 

,并在你的控制器:

.controller('myctrl',function($scope){ 
    var values=[]; 
    $scope.toggleCheckBox=function(isChecked, value, pos){ 
    if(isChecked){ 
     values[pos] = value; 
    }else{ 
     values[pos] = null; 
    } 
    console.log(values); 
    } 
}) 
1

从理论上讲,你可以在一个值添加到特定索引array:

let arr = ['foo', 'bar']; 
arr[3] = 'baz'; // 0 and 1 are taken 
console.log(arr); //=> ['foo', 'bar', undefined, 'baz'] 

但是,这并不推荐使用。在JavaScript中,数组是数字索引,尝试解决此问题很快就会出现问题。

你需要的是一个对象。如果你的名字你输入:

<input name="some_checkbox_1"> 

...并使用这些名字来创建一个对象:

$scope.toggleCheckBox=function(isChecked, value){ 
    values[this.name] = isChecked; 
} 

导致的对象是这样的:

{ some_checkbox_1: true, some_checkbox_2: false } // this is the `values` variable 

如果再想用真值和/或假值从这个对象中取出数组,请使用Object.values()

Object.values(values); //=> [true, false] 

这样,您可以按顺序渲染复选框,并根据存储对象的真/假值将它们设置为checked。希望这可以帮助。

注:以上我认为this指复选框的HTML元素的代码

1

JavaScript对象的属性顺序不会改变

所以,你可以创建一个对象来维持秩序,遍历Object.keys()填充阵列values基于valuefalse风琴:

angular 
    .module('ionicApp', ['ionic']) 
    .controller('myctrl', function ($scope) { 
    var values = [], 
     obj = {}; 

    $scope.toggleCheckBox = function (isChecked, value, index) { 
     values = []; // Clean the values 
     obj[index] = (isChecked) ? value : false; 
     Object.keys(obj).forEach(function (k) { 
      obj[k] && values.push(obj[k]); // Fill the ordered values 
     }); 

     console.clear(); 
     console.log(values); 
    }; 
    }); 

在veiew,我增加了一个index所有的复选框,这是ion-checkbox数量:

<ion-checkbox 
    ng-click="toggleCheckBox(success.first,values.first, 1)" 
    ng-change="show2='true'" 
    ng-model="success.first" 
    ng-disabled="!values.first" 
    style="border: none;padding-left: 30px;" 
    class="checkbox-royal"> 
</ion-checkbox> 

检查在Code Pen完整的解决方案。

+0

谢谢@Yosvel Quintero –

+0

但是,如果我改变字段的值,它只是将值推到数组的最后一个索引,而不是修改以前的值。 –

+0

您的权利..我没有考虑修改后的值..正在处理.. –