先看代码运行效果,输入相应的数据后点击开始计算,显示如下:
一开始先搭建好页面,这个界面用了插件bootstrap-3.3.7-dist,然后根据数学计算算出相关的本息,金额等数据,直接上代码:
页面搭建代码如下:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href="~/Content/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
<style>
.input-group-addon{
display:inline-block;
width:91px;
margin-left:-15px;
height:34px
}
</style>
</head>
<body>
<div style="margin:50px;">
<div class="panel panel-primary" style="border-color:darkgrey;width:80%;margin-left:140px;border: 1px solid rgba(0,0,0,.2);">
<div class="modal-header ">
<h4 class="modal-title" style="color:darkgrey;display:inline-block;">理财计算器</h4>
</div>
<div class="row">
<form id="form">
<div class="col-md-6">
<div class="form-group" style="margin:25px">
<div class="col-lg-3 col-md-3 col-sm-3 text-right" style="margin-top:7px">
<label class="control-label">投资金额</label>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 reset">
<input type="text" class="form-control" style="border-radius:4px 0 0 4px" id="Invest" onkeyup="value = value.replace(/[^\d.]/g, '')" />
</div>
<div class="input-group-addon" style="width:50px">
<span style="display:inline-block;margin-top:4px">元</span>
</div>
</div>
<div class="form-group" style="margin:25px">
<div class="col-lg-3 col-md-3 col-sm-3 text-right" style="margin-top:7px">
<label class="control-label">借款期限</label>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 reset">
<input type="text" class="form-control" style="border-radius:4px 0 0 4px" id="Deadline" onkeyup="value=value.replace(/[^\d]/g,'')" />
</div>
<div class="input-group-addon">
<div class="col-lg-8 col-md-8 col-sm-8 reset" style="margin-top:3px;margin-left:-10px">
<input type="radio" name="date" value="Moth" />月
<input type="radio" name="date" value="Day" />天
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group" style="margin:25px">
<div class="col-lg-3 col-md-3 col-sm-3 text-right" style="margin-top:7px">
<label class="control-label">年化收益</label>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 reset">
<input type="text" class="form-control" style="border-radius:4px 0 0 4px" id="Earnings" onkeyup="value = value.replace(/[^\d.]/g, '')" />
</div>
<div class="input-group-addon" style="width:50px">
<span style="display:inline-block;margin-top:4px">%</span>
</div>
</div>
<div class="form-group" style="margin:25px">
<div class="col-lg-3 col-md-3 col-sm-3 text-right" style="margin-top:7px">
<label class="control-label">还款方式</label>
</div>
<div class="col-lg-5 col-md-5 col-sm-5 reset">
<select type="text" class="form-control" id="Type">
<option value="1">按月付息,到期还款</option>
<option value="2">按天计息,到期还款</option>
</select>
</div>
</div>
</div>
</form>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 text-center" style="margin-top:10px;">
<button class="btn btn-info" type="button" id="StartCounting">开始计算</button>
<button class="btn btn-danger" type="button" id="Reset">重置数据</button>
</div>
</div>
<div class="row" style="margin-left:0;width:100%;">
<div style="margin-left:365px;margin-top:30px">
<div>
<label>投资金额:</label>
<input type="text" id="Investmoney" style="border:none;" readonly value="元"/>
<label style="display:inline-block;margin-left:60px;">利息收益:</label>
<input type="text" style="border:none;" id="LXSY" readonly value="元"/>
</div>
<div>
<label>投资期限:</label>
<input type="text" id="deadline" style="border:none;" readonly value=""/>
<label style="display:inline-block;margin-left:60px;">本息合计:</label>
<input type="text" style="border:none;" id="BXHJ" readonly value="元"/>
</div>
</div>
</div>
<div class="row" style="margin-left:0;width:100%;margin-top:35px;">
<table class="table" id="table">
<tr>
<th style="text-align:center">期数</th>
<th style="text-align:center">每月还款本息</th>
<th style="text-align:center">应还本金</th>
<th style="text-align:center">应还利息</th>
<th style="text-align:center">剩余回款本息</th>
</tr>
</table>
</div>
</div>
</div>
<script src="~/Content/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="~/Content/bootstrap-3.3.7-dist/js/jquery-2.0.3.min.js"></script>
</body>
</html>
然后下面是实现功能js代码,主要都是一些判断和计算:
//重置数据
var Reset = document.getElementById("Reset");
Reset.onclick = function () {
location.reload();
}
$("#StartCounting").click(function () {
var Invest = $("#Invest").val();//投资金额
$("#Investmoney").val(Invest + "元");//投资金额
var Date = $("#form input:checked").val();
var Deadline = $("#Deadline").val();//借款期限
if (Date == "Moth") {
$("#deadline").val(Deadline + "个月");//日期
var Annualized = $("#Earnings").val() / 100;
var AA = (Invest * Annualized) / 12 * Deadline;
$("#LXSY").val(AA + "元");//利息
$("#BXHJ").val(parseInt(Invest) + parseInt(AA) + "元");//本息合计
$("#Type").val(1);
var MYHKBX = (Invest * Annualized) / 12 + Invest / Deadline;
var YHBJ = Invest / Deadline;
var YHLX = (Invest * Annualized) / 12;
for (var i = 1; i < parseInt(Deadline) + 1; i++) {
var SYHKBX = parseInt(Invest) + parseInt(AA) - (MYHKBX * i);
$("#table").append("<tr><td style='text-align:center'>" + i
+ "</td><td style='text-align:center'>" + MYHKBX + "元"
+ "</td><td style='text-align:center'>" + YHBJ + "元"
+ "</td><td style='text-align:center'>" + YHLX + "元"
+ "</td><td style='text-align:center'>" + SYHKBX + "元" + "</td>");
}
}
if (Date == "Day") {
$("#deadline").val(Deadline + "天");//日期
var Annualized = $("#Earnings").val() / 100;
var BB = (Invest * Annualized) / 365 * Deadline;//利息
$("#LXSY").val(BB + "元");//利息
$("#BXHJ").val(parseInt(Invest) + parseInt(BB) + "元")//本息合计
$("#Type").val(2);
var MYHKBX = (Invest * Annualized) / 12 + Invest / Deadline;
var YHBJ = Invest / Deadline;
var YHLX = (Invest * Annualized) / 12;
for (var i = 1; i < parseInt(Deadline) + 1; i++) {
var SYHKBX = parseInt(Invest) + parseInt(BB) - (MYHKBX * i);
$("#table").append("<tr><td style='text-align:center'>" + i
+ "</td><td style='text-align:center'>" + MYHKBX + "元"
+ "</td><td style='text-align:center'>" + YHBJ + "元"
+ "</td><td style='text-align:center'>" + YHLX + "元"
+ "</td><td style='text-align:center'>" + SYHKBX + "元" + "</td>")
}
}
})