javascript - How to used jquery in a calculation
问题描述
I want the show the amount value using JQuery.
I have used jAutoCalc()
but it seems to be unsuitable with my format where some of the amount is fixed. I looked at this link https://forum.jquery.com/topic/calculation-on-form-fields but there is still something wrong which the amount changed after I changed the dayhour.
$(document).ready(function() {
var repeat = '<tr><td><input class="form-control" name="dayhour[]" type="number" id="dayhour" ></td><td><select class="form-control" name="wagetype[]" type="text" id="wagetype" onchange="calc(this)" ><option value="-----" id="-----" hidden>-----</option><option value="Normal days - OT" id="Normal days - OT">Normal days - OT</option><option value="Public Holidays - OT" id="Public Holidays - OT">Public Holidays - OT</option><option value="Attendance Allowance" id="Attendance Allowance">Attendance Allowance</option></select></td><td><input class="form-control" name="rate[]" type="double" id="rate" readonly ></td><td><input class="form-control" name="amount[]" type="double" id="amount" readonly></td><td><input class="btn btn-danger" type="button" name="add" id="remove" value="remove"></td></tr>';
$("#add").click(function() {
$("#table_field").append(repeat);
});
$("#table_field").on('click', '#remove', function() {
$(this).closest('tr').remove();
});
});
function calc(el) {
//get num1 field value
var num1 = parseFloat($(el).closest('tr').find('[name*=dayhour').val());
var amount;
var rate;
$('#amount').val(amount);
//compare
if (el.value == "Normal days - OT") {
rate = 10.00;
amount = rate * num1;
} else if (el.value == "Public Holidays - OT") {
rate = 20.00;
amount = rate * num1;
} else if (el.value == "Attendance Allowance") {
rate = 200.00;
amount = 200.00;
}
$('#num1').change(calc);
$(el).closest('tr').find('[name*=rate]').val(rate);
$(el).closest('tr').find('[name*=amount]').val(amount);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="insert-form" name="table_field">
<table class="table table-bordered" id="table_field">
<tr>
<th>dayhour</th>
<th>wagetype</th>
<th>Rate</th>
<th>Amount</th>
<th>Add or Remove</th>
</tr>
<tr>
<td><input class="form-control" name="dayhour[]" type="number" id="dayhour"></td>
<td>
<select class="form-control" name="wagetype[]" type="text" id="wagetype" onchange="calc(this)">
<option value="-----" id="-----" hidden>-----</option>
<option value="Normal days - OT" id="Normal days - OT">Normal days - OT</option>
<option value="Public Holidays - OT" id="Public Holidays - OT">Public Holidays - OT</option>
<option value="Attendance Allowance" id="Attendance Allowance">Attendance Allowance</option>
</select>
</td>
<td><input class="form-control" name="rate[]" type="double" id="rate" readonly></td>
<td><input class="form-control" name="amount[]" type="double" id="amount" readonly></td>
<td><input class="btn btn-warning" type="button" name="add" id="add" value="add"></td>
</tr>
</table>
</form>
解决方案
推荐阅读
- python - Python:使用 Tensorflow 的文本定位器
- function - ES6 导入函数 - 在 Vue 组件中执行
- reactjs - React Checkbox 没有获得价值 onCheck
- uwp - StorageFileQueryResult.GetFilesAsync() 在 UWP 中与 IndexerOption 正确使用
- angular - 如何在条件语句中异步 api 响应?
- java - 如何从另一个片段中对适配器进行排序
- java - 搜索并列出 .m2(本地)maven 存储库
- regex - 用一些不同的电子邮件域替换电子邮件域
- watchkit - SwiftUI 列表样式
- python - Ebuild 文件格式的正则表达式