2012-04-17 5 views
0

私はこれを行うと、手動でなぜ私のjavascript関数は、手動で入力を入力した場合にのみ機能するのですか?

function customer(){ 
this.monthlyBill = 300; 
} 

それが正常に動作した値を手動で私は毎月の請求書を設定する場合にのみ動作します

function doSomeMath(array,number){ 
    some math here..... 
} 

設定されている場合完璧に動作機能を持っている:

var someArray = [.2,.3,.6]; 
var customerData = new customer(); 
doSomeMath(someArray,customerData.monthlyBill); 

問題は手動で設定したくないということです。フォームの入力要素から値を取得したいのです。

私はこれを行うと、それは台無し:

function customer(){ 
this.monthlyBill = $('#monthly_bill').val(); 
} 

私は300で#monthly_billフォームとタイプに行くと、私は完全に別の値を取得します。私との違いは、それが文字列とみなされる第2のケースで

this.monthlyBill = 300 

this.monthlyBill = $('#monthl_bill').val(); // and then typing 300 into a form. 
+2

300は、数ある意味ので、誰かが有力0を入力する - xxx.val()文字列です - $( '#monthl_bill( 'のparseIntを試してみてください').val()、10); ' – mplungjan

答えて

3

this.monthlyBill = $('#monthl_bill').val(); 

を入力している何

。あなたは、整数

にそれを解析する必要がありますので、基本的には:

this.monthlyBill = parseInt($('#monthl_bill').val()); 
+0

Ahhhh ok。どうもありがとう! –

+1

+1ですが、 'parseInt'の基数引数を忘れないでください! –

+0

@JamesAllardiceは10以外の場合にのみ必要です。 –

0

私のコメントを投稿し、デモ

300は、数ある - )xxx.valは(文字列である - $(のparseIntを試してみてください( '#monthl_bill')。val()、10);

、10基数が場合に必要とされる8進数

DEMO

関連する問題