简体   繁体   English

用JavaScript制成的计算器,需要指定相对于自动输入金额的百分比

[英]Calculator made in JavaScript, Need to specify percentage to amount entered automatically

I have a calculator code that i am using on my site which is not working the way i want it to work. 我在网站上使用的计算器代码无法正常运行。 I have four plans all having different percentages given to different amounts 我有四个计划,每个计划的百分比都不同

First Plan : Amount Limit from 10 to 500 Second Plan : Amount Limit from 501 to 1000 Third Plan : Amount Limit from 1001 to 1500 Fourth Plan : Amount Limit from 1501 to 2000 第一个计划:金额限制从10到500第二个计划:金额限制从501到1000第三个计划:金额限制从1001到1500第四个计划:金额限制从1501到2000

It expect it to work in such a way that when an amount is entered it should automatically switch to the corresponding plan 它希望它以某种方式工作,以便在输入金额时应自动切换到相应的计划

For Example if 1200 is entered it should automatically change to Third Plan. 例如,如果输入1200,它将自动更改为“第三计划”。

$(function(){

    calc();
    enter code here
    $('#calc_plan').on('change', calc);
    $('#inv_amount').bind('change keyup', calc).on('keypress', isNumberKey);

});

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function calc() {

    var plan = $('#calc_plan').val();
    var amount = $('#inv_amount').val();
    var percent;

    switch (plan) {
        case '1':
            switch (true) {
                case (amount<=499):
                    percent = 104;
                    break;
                case (amount<=999):
                    percent = 105;
                    break;
                case (amount<=4999):
                    percent = 106;
                    break;
                case (amount<=9999):
                    percent = 107;
                    break;
                case (amount<=49999):
                    percent = 108;
                    break;
                case (amount<=99999):
                    percent = 109;
                    break;
                default:
                    percent = 109;
            }
            break;
        case '2':
            switch (true) {
                    case (amount<=499):
                    percent = 114;
                    break;
                case (amount<=999):
                    percent = 117;
                    break;
                case (amount<=4999):
                    percent = 120;
                    break;
                case (amount<=9999):
                    percent = 123;
                    break;
                case (amount<=49999):
                    percent = 126;
                    break;
                case (amount<=99999):
                    percent = 129;
                    break;
                default:
                    percent = 129;
            }
            break;
                case '3':
            switch (true) {
                    case (amount<=499):
                    percent = 124;
                    break;
                case (amount<=999):
                    percent = 131;
                    break;
                case (amount<=4999):
                    percent = 138;
                    break;
                case (amount<=9999):
                    percent = 145;
                    break;
                case (amount<=49999):
                    percent = 152;
                    break;
                case (amount<=99999):
                    percent = 159;
                    break;
                default:
                    percent = 159;
            }
            break;
        case '4':
            switch (true) {
                case (amount<=499):
                    percent = 154;
                    break;
                case (amount<=999):
                    percent = 175;
                    break;
                case (amount<=4999):
                    percent = 196;
                    break;
                case (amount<=9999):
                    percent = 217;
                    break;
                case (amount<=49999):
                    percent = 238;
                    break;
                case (amount<=99999):
                    percent = 259;
                    break;
                default:
                    percent = 259;
            }
            break;



    }

    $('#assign_per').val(percent+'%');
    var total = amount*percent/100;
    $('#total_return').val(total+'$');
    $('#net_profit').val((total-amount).toFixed(2)+'$');

}

According to what I understood from your question is that you want the amount to automatically select the plan based on the rule given whereas you are using variable for it. 根据我对您的问题的理解,您希望金额根据给定的规则自动选择计划,而您正在使用变量。

function check= function(amount,a,b){
 // check if the amount is in range a,b
 return (amount>=a && amount < b);
}
function calc() {

    var amount = $('#inv_amount').val();
    var percent;

     var plan = check(amount,10,500)?1:check(amount,500,1000)?2:check(amount,1000,1500)?3:0 (as much condition you wanna check)

    switch (plan) {
        case '1':
            switch (true) {
                case (amount<=499):
                    percent = 104;
                    break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM