首页 > 解决方案 > 使用 ng-repeat angularjs 时获取输入的值

问题描述

我将来自 api 的信息放入表中,我需要从输入中获取值。假设我有一个付款清单(从 api 获得),有两种选择:支付全部或支付一部分。

这是我的代码的一部分:

function searchPayments(){
  $http.get('theApi')
  .then(function(data){
     $scope.payments = data.data.Response;
}

然后,在我的 html 视图中:

<table class="table table-bordered">
   <thead>
      <tr>
         <th>Name</th>
         <th>Service</th>
         <th>Total</th>
         <th>Partial</th>
         <th>Amount</th>
      </tr>
   </thead>
   <tbody>
      <tr ng-repeat="pay in payments">
         <th>{{pay.Name}}</th>
         <th>{{pay.Service}}</th>
         <th>
           <input type="checkbox" name="totalCheck" class="form-control" 
                   ng-model="total" ng-disabled="partial"/>
         </th>
         <th>
           <input type="checkbox" name="partialCheck" class="form-control"
                  ng-model="partial" ng-disabled="total">
         </th>
         <th>
           <a ng-show="total">${{pay.AmountPay}}</a>
           <input type="text" class="form-control" placeholder="$"
                  ng-show="partial" style="width:100px;" id="importTo">
         </th>
      </tr>
   </tbody>
</table>

事情是这样的:如果用户选中“Total”复选框,在 Amount 行中将显示获得的金额(来自 api),如果选中“Partial”复选框,则会出现一个文本框,用户可以在其中输入“x”金额. 然后,我有一个按钮:

<button type="button" class="btn btn-primary"
         ng-click="getSpecificId(pay);associateThis()">
  Let's pay!
</button>

它调用了下一段代码:

$scope.getSpecificId = function(x){
    $scope.specificInfo = x.AmountPay;
    //Here's where i catch the especific quantity of the record what i want to pay
}

$scope.associateThis = function(){

        if($('input[name="totalCheck"]').is(':checked')){
            $scope.qtyOn = $scope.specificInfo;
            console.log($scope.qtyOn);
        }
        else if($('input[name="partialCheck"]').is(':checked')){*/
            $scope.qtyOn = document.getElementById('importTo').value;
            console.log($scope.qtyOn);
        }
        else{
            alert("Select Total or Partial checkbox!");
        }
    }

我的问题出现在检查部分复选框并按下按钮时,因为仅获取第一个记录的书面值(如果用户选择第一个记录)。如果用户选择另一条记录,则该复选框的值为空。

有人可以帮我获取价值吗?我正在使用 Javascript、AngularJs 和 Bootstrap。

提前谢谢。

标签: javascriptangularjs

解决方案


推荐阅读