当用户使用复选框选项选择行时,点击“计算折旧”,我想抓取{{asset.purchaseValue}},{{asset.residualValue}},{{asset.purchaseDate}}中的值和{{asset.ddate}}执行计算以发回给用户。如何在jQuery,AngularJS或vanilla Javascript中获取这些值?
<table align="center" class="assetlist">
<thead>
<tr>
<th width="45px">
<select ng-model="sortBy" class="localSearch">
<option value="title">Title</option>
<option value="ddate">Disposal Date</option>
<option value="date">Purchase Date</option>
<option value="residualValue">Residual Value</option>
<option value="purchaseValue">Purchase Value</option>
</select>
</th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Title" ng-model="searchString.title" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Date" ng-model="searchString.date" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Value" ng-model="searchString.purchaseValue" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Disposal Date" ng-model="searchString.ddate" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Residual Value" ng-model="searchString.residualValue" /></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" ng-model="checked"/></td>
<td>{{asset.title}}</td>
<td>{{asset.date | date: "MMMM d, y"}}</td>
<td>{{asset.purchaseValue | currency:"£"}}</td>
<td>{{asset.ddate | date: "MMMM d, y"}}</td>
<td>{{asset.residualValue | currency:"£"}}</td>
</tr>
</tbody>
</table>
<button class="formbtn" ng-disabled="!checked">Calculate Depreciation</button>
答案 0 :(得分:0)
当用户选中该框时,将该行传入:
HTML:
<input type="checkbox" ng-model="checked" ng-checked="evaluate(asset)"/>
JavaScript的:
$scope.evaluate = function(asset) {
//var purchaseValue = asset.purchaseValue
//var residualValue = asset.residualValue
//var purchaseDate = asset.purchaseDate
//var ddate = asset.ddate
//do calculations
}
答案 1 :(得分:0)
以下是所需输出的修改代码:(复制并试用!)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var purchaseValue,residualValue,life=1;
$(".ckb").on('click',function(){
var checked=$(this).is(':checked');
if(checked){
//checkbox checked
var tr=$(this).parent().parent().parent();
var titlevalue=tr.find('td:eq(1)').html();
purchaseValue=tr.find('td:eq(3)').html();
residualValue=tr.find('td:eq(5)').html();
}else{
//checkbox unchecked
}
});
$("#calculateId").on('click',function(){
if(residualValue==undefined&&residualValue==undefined){
alert('please select the checkbox');
}else{
// alert(purchaseValue);
//alert(residualValue);
var result=((parseInt(purchaseValue)-parseInt(residualValue)) /life);
alert('result:'+result);
}
});
});
</script>
</head>
<body>
<table align="center" class="assetlist">
<thead>
<tr>
<th width="45px">
<select ng-model="sortBy" class="localSearch">
<option value="title"> Title</option>
<option value="ddate"> Disposal Date</option>
<option value="date"> Purchase Date</option>
<option value="residualValue">residualValue</option>
<option value="purchaseValue">purchaseValue</option>
</select>
</th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Title" ng-model="searchString.title" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Date" ng-model="searchString.date" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Purchase Value" ng-model="searchString.purchaseValue" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Disposal Date" ng-model="searchString.ddate" /></th>
<th class="thead" ng-hide="hide"><input type="text" class="localSearch" placeholder="Residual Value" ng-model="searchString.residualValue" /></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" class="ckb" ng-model="checked"/></td>
<td>title1</td>
<td>date1</td>
<td>20</td>
<td>ddate1</td>
<td>10</td>
</tr>
</tbody>
</table>
</form>
<button class="formbtn" ng-disabled="!checked" id="calculateId">Calculate Depreciation</button>
</body>
</html>
答案 2 :(得分:0)
由于有多个资源,您应该在selected
<td>
属性为true(或切换) >
<tr ng-repeat="asset in assets | orderBy:sortBy | filter:searchString : searchStrict | limitTo: rowLimit">
<td><input type="checkbox" ng-model="asset.selected" /></td>
<td>{{asset.title}}</td>
<td>{{asset.date | date: "MMMM d, y"}}</td>
<td>{{asset.purchaseValue | currency:"£"}}</td>
<td>{{asset.ddate | date: "MMMM d, y"}}</td>
<td>{{asset.residualValue | currency:"£"}}</td>
</tr>
<button class="formbtn" ng-click="calculateDep()">Calculate Depreciation</button>
在控制器中,
//For Calculation
$scope.calculateDep = function() {
angular.forEach($scope.assets, function(asset) {
if (asset.selected) {
//Here are asset.purchaseValue,asset.residualValue ...
//perform calculation individually for an asset
//or send the enitire selected assets for calculation by creating a new scope array for selected ones
}
})
}
<强>更新强>
请参阅下面的代码段,当您选择一个或多个复选框并计算dep时,您可以在控制台中看到整个对象。
angular.module("app", []).controller("ctrl", function($scope) {
$scope.assets = [{
title: 'Asset1',
date: '',
purchaseValue: 500,
ddate: '',
residualValue: 100
}, {
title: 'Asset2',
date: '',
purchaseValue: 500,
ddate: '',
residualValue: 100
}];
//For Calculation
$scope.calculateDep = function() {
console.clear();
angular.forEach($scope.assets, function(asset) {
if (asset.selected) {
console.log(asset);
}
})
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="asset in assets track by $index">
<td><input type="checkbox" ng-model="asset.selected" /></td>
<td>{{asset.title}}</td>
<td>{{asset.date}}</td>
<td>{{asset.purchaseValue}}</td>
<td>{{asset.ddate}}</td>
<td>{{asset.residualValue}}</td>
</tr>
</table>
<button class="formbtn" ng-click="calculateDep()">Calculate Depreciation</button>
</body>