2016-09-22 57 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

辉煌,谢谢 –