buy-vs-rent
Version:
133 lines (120 loc) • 4.05 kB
HTML
<html lang="cmn-hans">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="format-detection" content="telephone=no,email=no,address=no">
<title>buy-vs-rent demo</title>
<style>
label {
display: block;
padding: 1px;
}
p {
padding: 5px;
}
</style>
</head>
<body>
<form action="">
<label for="loan">
贷款量(万)
<input type="number" id="loan" name="loan">万
</label>
<label for="loanRate">
贷款年利率
<input type="number" id="loanRate" name="loanRate" step="0.0001">
</label>
<label for="months">
还贷时间(月)
<input type="number" id="months" name="months">月
</label>
<hr>
<label for="cash">
首付|现金量(万)
<input type="number" id="cash" name="cash">万
</label>
<label for="cashRate">
现金年投资回报率
<input type="number" id="cashRate" name="cashRate" step="0.0001">
</label>
<label for="rent">
租房租金(万)
<input type="number" id="rent" name="rent" step="0.01">万
</label>
<label for="rentRate">
租房租金年涨幅率
<input type="number" id="rentRate" name="rentRate" step="0.0001">
</label>
<hr>
<label for="inflationRate">
年通货膨胀率
<input type="number" id="inflationRate" name="inflationRate" step="0.0001">
</label>
<hr>
<input type="submit" value="计算">
</form>
<p id="p"></p>
<script src="//unpkg.com/buy-vs-rent"></script>
<!-- <script src="../dist/buy-vs-rent.js"></script> -->
<script>
/**
* 获取某search值
* @param {String} checkKey - search的key
* @param {String} [search = window.location.search] - search总字符串
* @returns {String|Boolean} - search的value 或 不存在false
*/
window.getSearchValue = function (checkKey, search) {
checkKey = checkKey.toString()
search = search || window.location.search
if (search.slice(0, 1) === '?') {
search = search.slice(1)
}
var searchArr = search.split('&')
var tempArr, key, value
for (var i = 0, len = searchArr.length; i < len; i++) {
if (searchArr[i] !== '') {
tempArr = searchArr[i].split('=')
key = tempArr.shift()
value = tempArr.join('=')
if (key === checkKey) {
return value
}
}
}
return false
}
// 获取填入内容并打印
var loan = document.getElementById('loan')
var loanRate = document.getElementById('loanRate')
var months = document.getElementById('months')
var cash = document.getElementById('cash')
var cashRate = document.getElementById('cashRate')
var rent = document.getElementById('rent')
var rentRate = document.getElementById('rentRate')
var inflationRate = document.getElementById('inflationRate')
loan.value = window.getSearchValue('loan') || '300'
loanRate.value = window.getSearchValue('loanRate') || '0.049'
months.value = window.getSearchValue('months') || '360'
cash.value = window.getSearchValue('cash') || '100'
cashRate.value = window.getSearchValue('cashRate') || '0.04'
rent.value = window.getSearchValue('rent') || '0.4'
rentRate.value = window.getSearchValue('rentRate') || '0.1'
inflationRate.value = window.getSearchValue('inflationRate') || '0.05'
var text = window.buyvsrent({
loan: parseFloat(loan.value, 10),
loanRate: parseFloat(loanRate.value, 10),
months: parseFloat(months.value, 10),
cash: parseFloat(cash.value, 10),
cashRate: parseFloat(cashRate.value, 10),
rent: parseFloat(rent.value, 10),
rentRate: parseFloat(rentRate.value, 10),
inflationRate: parseFloat(inflationRate.value, 10)
}).msg
document.getElementById('p').innerHTML = text
console.log(text)
</script>
</body>
</html>