2017-10-21 18 views
0

私はフォームを持つテーブルを持っています。選択変更時にjQueryで2つの選択値を掛ける

各フォームの変更時に単価で数量を掛けて、その結果を入力したいと考えています。

$('select.BIL_item_quantity, select.BIL_item_id').on('change', function() { 
    var quantity = $(this).closest('select.BIL_item_quantity').find(':selected').val(); 
    var rate = $(this).closest('select.BIL_item_id').find(':selected').data('item-rate'); 
    $(this).closest('tr').find('input.BIL_item_total_rate').val(quantity*rate); 
}); 

しかし、それは動作しません:

私のJSコードは、実際にはこの1です。

お願いします。

ありがとうございました。

https://jsfiddle.net/uvdv812z/1/

答えて

1

いくつかの変更と最適化:

$('select.BIL_item_quantity, select.BIL_item_id').on('change', function() { 
    var parent = $(this).parents("tr:first"); 
    var quantity = parent.find('select.BIL_item_quantity').val(); 
    var rate = parent.find('select.BIL_item_id').find(':selected').data('item-rate'); 
    parent.find('input.BIL_item_total_rate').val((parseInt(quantity) * parseInt(rate)).toFixed(2)); 
    }); 

デモ: https://jsfiddle.net/lotusgodkk/uvdv812z/3/

+0

ホーすごいです!それは働いている。ありがとう。最後のもの:合計がカンマの後に数字を持たない場合、どうやって '.00'を最後に保つことができますか? – user8201518

+0

@ user8201518 parseIntの代わりにparseFloatを使用します。 –

+0

これは同じです。 :( – user8201518

関連する問題