<table class="wareList">
<thead>
<tr>
<th>递增顺序</th>
<th>递增时间</th>
<th>递增金额</th>
<th width="200">递增标准</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2018-06-04</td>
<td><span class="moneyTxt">5000</span>元/月</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>2018-06-04</td>
<td><span class="moneyTxt">5000</span>元/月</td>
<td>
<div class="standardSet">
<span data-name="per" class="standName standBor standAction JS_standOpt">百分比</span>
<span data-name="money" class="standName standBor JS_standOpt">金额</span>
<span class="standName optValue"><input class="standPay" name="" type="text" onkeyup="inputNumberNew(event,this);" onblur="countRent(this)" value=""><em class="standUnit">%</em></span>
</div>
</td>
</tr>
<tr>
<td>3</td>
<td>2018-06-04</td>
<td><span class="moneyTxt">5000</span>元/月</td>
<td>
<div class="standardSet">
<span data-name="per" class="standName standBor standAction JS_standOpt">百分比</span>
<span data-name="money" class="standName standBor JS_standOpt">金额</span>
<span class="standName optValue"><input class="standPay" name="" type="text" onkeyup="inputNumberNew(event,this);" onblur="countRent(this)" value=""><em class="standUnit">%</em></span>
</div>
</td>
</tr>
<tr>
<td>4</td>
<td>2018-06-04</td>
<td><span class="moneyTxt">5000</span>元/月</td>
<td>
<div class="standardSet">
<span data-name="per" class="standName standBor standAction JS_standOpt">百分比</span>
<span data-name="money" class="standName standBor JS_standOpt">金额</span>
<span class="standName optValue"><input class="standPay" name="" type="text" onkeyup="inputNumberNew(event,this);" onblur="countRent(this)" value=""><em class="standUnit">%</em></span>
</div>
</td>
</tr>
</tbody>
</table>
HTML
$(function () {
//递增标准选择
$('.wareList').on('click', '.JS_standOpt', function () {
var optObj = $(this);
var standOpt = optObj.data('name');
var inputObj = optObj.parents('.standardSet').find('.standPay');
optObj.addClass('standAction').siblings().removeClass('standAction');
if (standOpt == 'per') {
inputObj.val('');
optObj.parents('.standardSet').find('.standUnit').text('%');
countRent(inputObj);
} else if (standOpt == 'money') {
inputObj.val('');
optObj.parents('.standardSet').find('.standUnit').text('元');
countRent(inputObj);
}
});
});
// 计算押金递增
function countRent(that) {
var money; // 递增后金额
var initialAmount = parseInt($(that).closest('tr').prev().find('.moneyTxt').text()); // 上一行tr的押金金额
var increaseNum = parseInt($(that).val().replace(/^\s+|\s+$/gm,'')); // 递增数
var increaseWay = $(that).parent('span').parent('div').find('.standAction').attr('data-name'); // 递增方式(%或固值)
var moneyTxt = $(that).closest('td').siblings().find('.moneyTxt'); // 押金显示span
if ($(that).val() != '') { // 判断递增方式是否为空
if (increaseWay == 'per') { // 判断递增方式
money = initialAmount * (1 + increaseNum / 100);
}else {
money = initialAmount + increaseNum;
}
}else {
money = initialAmount;
}
moneyTxt.text(parseInt(money));
// 遍历之后的tr
afterTr = $(that).closest('tr').nextAll();
afterTrEach(afterTr);
}
// 遍历之后的tr
function afterTrEach(afterTr) {
afterTr.each(function () {
var $increaseInput = $(this).find('.standPay');
var $currentLine = $(this).find('.moneyTxt');
var currentLineMoney = parseInt($currentLine.text()); // 计算后的押金
var increaseNum = parseInt($increaseInput.val()); //递增后金额
var moneyTxt = $(this).find('.moneyTxt'); // 押金显示span
var money;
var increaseWay = $(this).find('.standAction').attr('data-name'); // 递增方式(%或固值)
var initialAmount = parseInt($(this).prev().find('.moneyTxt').text()); // 上一行金额
if ($increaseInput.val() != '') {
if (increaseWay == 'per') { // 判断递增方式
money = initialAmount * (1 + increaseNum / 100);
} else {
money = initialAmount + increaseNum;
}
}
else {
money = initialAmount;
}
moneyTxt.text(parseInt(money));
})
}
运行结果
网友评论