2016-09-22 12 views
0

私は以下のコードを持っています。私は、現在の形式で無効入力ボックスに出力をフォーマットしますが、次のエラーを取得しようとしています番号を通貨としてフォーマットするJavascript

<select id="cage_options"> 
    <option>Please select</option> 
    <option value="2,63330">option1</option> 
    <option value="3,13300">option2</option> 
    <option value="4,2000.00">option3</option> 
</select> 
<input id="cage_labor_estimate" disabled> 

<script> 
    function format(n) { 
     return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"); 
    } 

    document.getElementById("cage_options").addEventListener("change", cageOptionFunction); 

    function cageOptionFunction() { 

     select=document.getElementById('cage_options').value.split(','); 

     cage_option = select[1]; 

     document.getElementById("cage_labor_estimate").value = format(cage_option); 
    } 

</script> 

TypeError: n.toFixed is not a function. (In 'n.toFixed(2)', 'n.toFixed' is undefined)

誰でも助けることができますか?

おかげで、

ジョン

+0

あなたは、文字列ではなく、番号を持っているので。 – epascarello

答えて

3

あなたは、文字列を持っていると、文字列は().toFixedを持っていません。その文字列を数値に変換する必要があります。

cage_option = Number(select[1]); 

または

cage_option = parseFloat(select[1]); 
+0

華麗な、ありがとう –

関連する問題