我有一个简单而奇怪的问题。 我有一个用 ng-repeat 制作的下拉列表。 (代码下方)
<ul>
<li ng-repeat="item in formLIST.ContractType">
<input type="checkbox" ng-click="checkItems(item)" value={{item.ls_ItemIndex}} >{{item.ls_ItemValue}}
</li>
</ul>
这是我的 Angularjs 代码(下)
$scope.listitem=[];
$scope.checkItems = function (item) {
$scope.listitem.push({
item: item.ls_ItemValue
});
我的问题是: 我想知道,当我选中或取消选中这些漂亮的小框时,它会在我的 JSON 文件中添加或删除它的值。
现在当我点击我的复选框时,它会被添加到我的 JSON 中。但是如果我再次单击它取消选中它,它将再次添加到我的 JSON 文件中。 我的代码可以多次添加它们。除了删除它。 我只想添加一次这些美丽的东西。
最佳答案
这里:
$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);
}
关于javascript - 将我的 CheckBox 列表值发送到 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24773644/