Javascript 将我的复选框列表值发送到Json
我有一个简单而奇怪的问题。 我有一个我用ng repeat制作的下拉列表。(以下代码) 我的问题是: 我想知道,当我选中或取消选中这些漂亮的小框时,它会在我的JSON文件中添加或删除它的值 现在,当我点击我的复选框时,它将被添加到我的JSON中。但如果我再次单击它以取消选中它,它将再次添加到我的JSON文件中。 我的代码可以多次添加它们。除了把它拿走。 我只想添加一次这些美丽的东西 这里:Javascript 将我的复选框列表值发送到Json,javascript,json,angularjs,checkbox,Javascript,Json,Angularjs,Checkbox,我有一个简单而奇怪的问题。 我有一个我用ng repeat制作的下拉列表。(以下代码) 我的问题是: 我想知道,当我选中或取消选中这些漂亮的小框时,它会在我的JSON文件中添加或删除它的值 现在,当我点击我的复选框时,它将被添加到我的JSON中。但如果我再次单击它以取消选中它,它将再次添加到我的JSON文件中。 我的代码可以多次添加它们。除了把它拿走。 我只想添加一次这些美丽的东西 这里: $scope.checkItems = function (item) { var idx = $
$scope.checkItems = function (item) {
var idx = $scope.listitem.indexOf(item.ls_ItemIndex);
if(idx == -1) // not selected
$scope.listitem.push(item.ls_ItemIndex);
else // selected
$scope.listitem.splice(idx,1);
}
我建议您使用ng型号:
<ul>
<li ng-repeat="item in formLIST.ContractType">
<input type="checkbox" ng-model="item.Is_ItemValue" />{{item.ls_ItemValue}}
</li>
</ul>
<!-- selected list -->
<ol>
<li ng-repeat="item in formLIST.ContractType | filter: {Is_ItemValue:true}"> {{ item }}</li>
</ol>
-
{{item.ls_ItemValue}}
{{item}
在此检查如何仅在值不存在时推送到数组。因此,如果选择了,则要推送,如果已经存在,则要弹出?我想您可能需要:
$scope.checkItems = function (item) {
var idx = $scope.listitem.indexOf(item.ls_ItemIndex);
if(idx == -1) // not selected
$scope.listitem.push(item.ls_ItemIndex);
else // selected
$scope.listitem.splice(idx,1);
}
<ul>
<li ng-repeat="item in formLIST.ContractType">
<input type="checkbox" ng-model="item.Is_ItemValue" />{{item.ls_ItemValue}}
</li>
</ul>
<!-- selected list -->
<ol>
<li ng-repeat="item in formLIST.ContractType | filter: {Is_ItemValue:true}"> {{ item }}</li>
</ol>