【问题标题】:send my CheckBox list value to a Json将我的 CheckBox 列表值发送到 Json
【发布时间】:2014-09-06 13:26:32
【问题描述】:

我有一个简单而奇怪的问题。 我有一个使用 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 文件中。 我的代码可以多次添加它们。除了删除它。 我只想添加这些美丽的东西一次。

【问题讨论】:

标签: javascript json angularjs checkbox


【解决方案1】:

这里:

$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);
}

【讨论】:

    【解决方案2】:

    我建议你使用 ng-model:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多