2016-04-01 22 views
0

私はこのコードを持っている:JavaScriptで2つの入力を関連付けるにはどうすればよいですか?

$(document).ready(function(){ 
 
    $('.container select').each(function(){ 
 
    \t \t $(this).on('change', function(){ 
 
     \t \t var selectedVal = $(this).val(); 
 
      //console.log(selectedVal); 
 
     }); 
 
    }); 
 
$('input').on('change', function() { 
 
    var sum = 0; 
 
    $('input').each(function() { 
 
     sum += Number($(this).val()); 
 
    }); 
 
\t $('.total span').html(sum); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class='container'> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select1'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
     </select> 
 
    </div> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select2'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
     </select> 
 
    </div> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select3'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
     </select> 
 
    </div> 
 
    <div class='total'> 
 
    Total nr: <span>5(2 A1, 3 A2)</span> 
 
    </div> 
 
</div>

は、それは選択の変化とタイプ番号の入力にはJavaScript/jQueryのを使用して上記のコードのように合計数を変更することは可能ですか? 誰でもこの助けてください。

入力フィールドまたは選択フィールドが変更されるたびに、A1の合計数とA2の合計数を計算する必要があります。これが意味をなさないことを望みます。総数の横にそれらを表示します。

JSFiddle

+0

私はあなたが達成したいものが得られません....総数は選択した値の結果でなければなりませんか? – messerbill

+0

各入力タイプ番号から合計値を計算する必要があります。また、選択に応じて、選択された各オプションの数。私はこれが意味をなさないことを願っています。 – Ionut

+0

@messerbill、私は少し質問を更新しました。たぶんあなたはもっとよく理解しているでしょう。 – Ionut

答えて

1

私たちはあなたの完全なコードを与えることはできませんが、私はあなたがこのようないくつかのことをしたいと思いますwant.I何のためにいくつかのロジックを提供しようとした:

//create a json to collect the sum of numbers 
 
var number = { 
 
    a1: 0, 
 
    a2: 0, 
 
    a1_count:0, 
 
    a2_count:0 
 
}; 
 
//check in select change 
 
$(".select").change(function() { 
 

 
//flush previous calculation before using again 
 
    number.a1=0,number.a2=0,number.a1_count=0,number.a2_count=0; 
 
    
 
//check all the select value and get the corresponding input value 
 
    $(".select").each(function() { 
 
    var valueType = $(this).val(); 
 
    if (valueType == "a1") { 
 
     number[valueType+"_count"]=number[valueType+"_count"]+1; 
 
     number[valueType] = number[valueType] + parseInt($(this).prev().val()||0); 
 
    } else if (valueType == "a2") { 
 
number[valueType+"_count"]=number[valueType+"_count"]+1; 
 
     number[valueType] = number[valueType] + parseInt($(this).prev().val()||0); 
 
    } 
 

 
    }); 
 
    
 
    $("#total").html('Total:'+(number.a1+number.a2)+',A1:'+number.a1+'('+number.a1_count+'),A2:'+number.a1+'('+number.a2_count+')'); 
 
    
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class='container'> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select1' class='select'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
    </select> 
 
    </div> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select2' class='select'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
    </select> 
 
    </div> 
 
    <div class='full'> 
 
    <input type='number' value=''> 
 
    <select name='select3' class='select'> 
 
     <option value='a1'>A1</option> 
 
     <option value='a2'>A2</option> 
 
    </select> 
 
    </div> 
 
    <div class='total' id="total"> 
 
    
 
    </div> 
 
</div>

+0

私は質問にいくつか変更を加え、余分なコードを追加しました。私は合計を計算する方法を知っています。私は合計を計算するのと同じ方法で、選択された値の合計数を総括的にparanthesisで追加する必要があります。 @Anand Sing、更新された質問にJsFiddleを追加しました。 – Ionut

+0

今編集したチェック –

+0

それはcorectlyでは動作しないようです。 – Ionut

0

これはselectの任意のリストで有効です。

function change() { 
    var res = {}, fulls = $('.container .full'); 
    fulls.each(function(index){ 
    var selected = $(this).find('select > option:selected').text(); 
    if (! res[selected]) res[selected] = 0; 
    res[selected] += 1*$(this).find('input[type="number"]').val(); 
    }); 
    var detail = "", total = 0; 
    for(prop in res) { 
    if (res.hasOwnProperty(prop)) { 
     try { 
     var val = 1*res[prop]; 
     detail += ", "+val+" "+prop; 
     total += val; 
     }catch(e){} 
    } 
    } 
    $('.total > span').text(""+total+" ("+detail.substr(2)+")"); 
} 

$().add('.container .full input[type="number"]') 
    .add('.container .full select[name^="select"]') 
    .on('change', change); 
+0

エラーを修正しました。 –

関連する問題