console
//提成表
var dic=[
{
price_min:0,
price_max:10,
royaltyRote:0.1
},
{
price_min:10,
price_max:20,
royaltyRote:0.075
},
{
price_min:20,
price_max:40,
royaltyRote:0.05
},
{
price_min:40,
price_max:60,
royaltyRote:0.03
},
{
price_min:60,
price_max:100,
royaltyRote:0.015
},
{
price_min:100,
price_max:-1,
royaltyRote:0.01
}
];
//根据利润获取提成
function getRoyalty(price){
var royalty=0;
for(var i=0;i<dic.length;i++){
var rule=dic[i];
var tempPrice=0;
if(price>=rule.price_min){
if(rule.price_max==-1 || price<=rule.price_max){
tempPrice=price-rule.price_min;
}else{
tempPrice=rule.price_max-rule.price_min;
}
royalty+=tempPrice*rule.royaltyRote;
}else{
break;
}
}
return royalty;
}
do function(){
var price=document.getElementById("price").value;
document.getElementById("royalty").value=getRoyalty(price);
}
<html>
<body>
利润:<input type="text" id="price" value="15">
<button onclick="do()">计算</button>
提成:<input type="text" id="royalty" value="">
</body>
</html>