2016-12-05 6 views
0

問題があります。私は単純な電卓のスクリプトを持っていて、スクリプトは間違って計算し始めました。私が "1000"を入力すると右($ 10)が計算され、50000と入力すれば右($ 380)も計算されます。しかし、10000と入力すると間違って計算されます。電卓のスクリプトが間違って範囲を数えます

範囲は次のとおりです。

0 – 1000 = $0.01 
10000 – 10000 = $0.009 
10000 – 25000 = $0.0084 
25000 – 50000 = $0.0076 
50000+ = $0.0076 

私はあなたの助けのために感謝しますので、残念ながら私はJavascriptの専門家ではありません。

function priceCalculation(a){ 
 
    if(a <= 1000){ 
 
     return 0.001; 
 
    }else if(a >= 1001 && a <= 10000){ 
 
     return 0.009; 
 
    }else if(a >= 10001 && a <= 25000){ 
 
     return 0.0084; 
 
    }else if(a >= 25001 && a <= 50000){ 
 
     return 0.0076; 
 
    }else{ 
 
     return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e){ 
 
    // if a '.' is pressed 
 
\t if($(this).val().endsWith('.')) { 
 
    \t return; 
 
    } 
 

 
    // if input value is empty then assign '0' else the original value 
 
    var inputVal = $(this).val() === ''?'0':$(this).val(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation($(this).val()); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text((total)); // display the total price 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p>

+0

「10,000」と入力すると入力が「1,0000」となります – philantrovert

答えて

1

少しミスを犯しました。

var price = priceCalculation($(this).val()); 

$(this).val()は文字列を返します。あなたは(何千もの)その中に,を持っているとき、それはあなたがすでに数に解析されてきたinputValを持っているので、あなたのpriceCalculation関数を呼び出すとreturn 0.0076;

で最後else文を返すようになり、あなたが好きそれを使用することができ、

var price = priceCalculation(inputVal); 

最終結果、

function priceCalculation(a) { 
 
    if (a <= 1000) { 
 
    return 0.001; 
 
    } else if (a >= 1001 && a <= 10000) { 
 
    return 0.009; 
 
    } else if (a >= 10001 && a <= 25000) { 
 
    return 0.0084; 
 
    } else if (a >= 25001 && a <= 50000) { 
 
    return 0.0076; 
 
    } else { 
 
    return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e) { 
 
    if ($(this).val().endsWith('.')) { 
 
    return; 
 
    } 
 
    
 
    var inputVal = $(this).val() === '' ? '0' : $(this).val(); 
 

 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation(inputVal); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal); 
 
    $(this).val(formatted); 
 
    $('#output').text((total)); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b> 
 
</p>

+0

ありがとう、@choz!それは完全に動作します。 – Morgari

1

あなたはこの行が無効である

function priceCalculation(a){ 
 
    if(a <= 1000){ 
 
     return 0.001; 
 
    }else if(a >= 1001 && a <= 10000){ 
 
     return 0.009; 
 
    }else if(a >= 10001 && a <= 25000){ 
 
     return 0.0084; 
 
    }else if(a >= 25001 && a <= 50000){ 
 
     return 0.0076; 
 
    }else{ 
 
     return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e){ 
 
    // if a '.' is pressed 
 
\t if($(this).val().endsWith('.')) { 
 
    \t return; 
 
    } 
 

 
    // if input value is empty then assign '0' else the original value 
 
    var inputVal = $(this).val() === ''?'0':$(this).val(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation(inputVal); //a little mistake ;D 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text((total)); // display the total price 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p>

関連する問題