jquery复选框与select嵌套

时间:2013-06-12 12:43:39

标签: javascript jquery html

我有选中嵌套的复选框来更改范围值,但我的问题是当我有两个或更多选择时...我不擅长javascript = /

如何解决问题:在第二次选择中选择2之后选择第一个选择值2,然后选择第二个0和第一次选择0总计必须为0但我不知道,怎么做它= /

<div id="test_div">
<input class="test_class" type="checkbox" value="3,00"/>3,00
<input class="test_class" type="checkbox" value="5,00"/>5,00
<input class="test_class" type="checkbox" value="10,00"/>10,00
<input class="test_class" type="checkbox" value="15,00"/>15,00

<select class="test_class">
    <option value="10,00">0</option>
    <option value="10,00">1</option>
    <option value="10,00">2</option>
</select>
<select class="test_class">
    <option value="10,00">0</option>
    <option value="10,00">1</option>
    <option value="10,00">2</option>
</select>
<select class="test_class">
    <option value="10,00">0</option>
    <option value="10,00">1</option>
    <option value="10,00">2</option>
</select>
<span id="total">0,00</span><br/>
</div>

JS:

$(function()
{
   var last = 0;
   $('body').on('change','.test_class', function()
    {
        var vt = parseFloat($('#total').html().replace(',','.'));
        if($(this).is('input:checked'))
        {
           var val = parseFloat($(this).val().replace(',','.'));
           total = val + vt;
        }else if($(this).is('input'))
        {
            var val = parseFloat($(this).val().replace(',','.'));
            total = vt - val;
         }
    if($(this).is('select'))
    {
        var val = parseFloat($(this).val().replace(',','.'));
        var index = $(this).prop('selectedIndex');

    if(index == 0)
    {
        if(last > 0)
        {
            total = vt - val*last;
        }
        last = 0;
    }

    if(index == 1)
    {
        if(last == 2)
        {
            total = vt - val;
        }else
        {
            total = vt + val*index;
        }
        last = 1;
     }

     if(index == 2) 
     {
         if(last == 1)
         {
             total = vt + val;
         }else
         {
             total = vt + val*index;
         }
         last = 2;      
     } 
    }
    total = total.toFixed(2).replace('.',',');
    $('#total').html('').html(total);
    });    
});

示例:http://jsfiddle.net/jvtcc/8/

1 个答案:

答案 0 :(得分:0)

$("select .test_class").change(function() {
    var total;
    total = 0;
    $("select .test_class").each(function() {
        total += parseInt($(this).value);
    });
    $("#total").text(total + ".00");
});