2016-04-01 15 views
0

私は4つのテキストボックス、すなわちcustomer_phy_tot,customer_che_totcustomer_bio_totを持っています。私は3つの入力ボックスの値をすべて追加して4番目の入力customer_pcb_totに表示しようとしています。入力値の追加方法

customer_bio_obt.blur(function(){ 
    var customer_pcb_tot = isNaN(parseInt($("#customer_phy_tot").val() + $("#customer_che_tot").val() + $("#customer_bio_tot").val())) ? 0 :($("#customer_phy_tot").val() + $("#customer_che_tot").val() + $("#customer_bio_tot").val()) 
    $("#customer_pcb_tot").val(customer_pcb_tot); 
}); 

問題は私のコードを追加するのではなく、文字列として扱います。私が値10,15および5をそれぞれ持っているとします。 4番目のボックスに30と表示されるはずですが、私の場合は10155と表示されています。

大変助かりました。あなたはそれが動作しますか、あなたの値のそれぞれにそれを使用する必要が

+1

使用するparseInt(0):$( "#customer_phy_tot"すべての 'val()+ val() 'のための別の値のために – teran

+0

' total =(parseInt($( "#somtthing")val())|| 0)+ ... ' – teran

答えて

3

あなたはすでにparseInt()を使用しているが、あなたは、個々のval()呼び出しによって返される文字列値にそれを使用する必要があります。また、三項式の使用は冗長であることに注意してください。これを試してください:

customer_bio_obt.blur(function() { 
    var customer_pcb_tot = parseInt($("#customer_phy_tot").val(), 10) + parseInt($("#customer_che_tot").val(), 10) + parseInt($("#customer_bio_tot").val(), 10); 
    $("#customer_pcb_tot").val(customer_pcb_tot || 0); 
}); 
3

使用これを行うにはparseInt()機能:)

として@Jamie R Rytlewskiは言った:

あなたはまた、基数値を使用していることを確認してください。 parseInt(文字列、基数);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

+0

私は運を試みましたが、 – Sha

+2

ありがとうございますが、基数の値も必ず使用してください。 parseInt(文字列、基数); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt –

+0

基数はデフォルトで10です – teran

0

あなたが求める各val()でparseInt()を実行できます。 または、それぞれを1に掛けてから合計してください。

var customer_pcb_tot = isNaN(parseInt($( "#customer_phy_tot"))val()+ $( "#customer_che_tot")val()+ $( "#customer_bio_tot")val()))? val()* 1)

関連する問題