【问题标题】:Angular way to fetch checkbox value and the corresponding textbox value获取复选框值和相应文本框值的角度方式
【发布时间】:2017-02-19 14:02:40
【问题描述】:

我有一个简单的表单,其中我有一个带有有意义名称的文本框,并且对应于每个我有一个文本框。

我可以按照建议获取复选框列表及其 ID,但无法获取其对应的文本框值。

有什么指导可以实现这一点吗?仅供参考,复选框的值来自数据库并通过 ng-repeat 进行迭代。

这里是代码sn-p

<tr ng-repeat="item in itemList | filter:search_query">
    <td>
     <label for="{{item.inventoryId}}"> 
     <input type="checkbox" class="checkbox-circle" 
     ng-click="materialSelected(materialSelection.ids, item.inventoryId);"  
     ng-model="materialSelection.ids[item.inventoryId]" name="group" id="
    {{item.inventoryId}}" /> {{item.itemName}}
   </label>
   </td>
   <td><span>
   <label>
   <input type="text" class="form-control" ng-model="item.requiredQty">                    
   </label></span>
</td>

Javascript 代码

$scope.materialSelected = function(idsList, inventoryId) {
                    $scope.materialList = [];
                    var materialId;
                    for (materialId in idsList) {
                        if (idsList[materialId] == true) {
                            $scope.materialList.push({
                                'materialId' : parseInt(materialId, 10)
                            });
                        }
                    }
                }

【问题讨论】:

  • 你能显示你的代码sn-p
  • 我已将 HTML 和 JS 代码添加到我的帖子中。请看一下
  • @CrazyMac 你有什么运气让它工作吗?我能以其他方式提供帮助吗?
  • @TimHarker 感谢您的跟进。我做了一些类似于我在下面发布的操作,它对我有用。

标签: javascript html angularjs


【解决方案1】:

如果我理解正确,你可以试试这个……

$scope.materialList.push({
    'materialId' : parseInt(materialId, 10),
    'requiredQty' : $scope.itemList.find(x => x.inventoryId == materialId).requiredQty
});

或者这个……

$scope.materialList.push({
    'materialId' : parseInt(materialId, 10),
    'requiredQty' : $scope.itemList.filter(function(v) { return v.inventoryId == materialId; })[0].requiredQty
});

取决于需要的浏览器支持。

【讨论】:

    【解决方案2】:

    在 HTML 元素中,我添加下面的代码,

    <span ng-repeat="item itemList" style="text-align: left;"> 
    <label for="{{item.itemID}}"> 
    <input type="checkbox" class="checkbox-circle" ng-click="itemSelected(selection.ids, item.itemID)" style="border-radius: 2px;" ng-model="selection.ids[item.itemID]" name="group" id="{{item.itemID}}" /> {{item.itemName}} 
    </label> 
    </span>
    

    在 JS 中,我必须添加一个函数来捕获它,

    function(idsList) {
    for (plantID in idsList) {
      if (idsList[plantID] == true) {
      }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多