2015-05-29 14 views
1

我的計算工作不太好。也許你能幫我:在jquery中輸入值的多個選擇器

<input id="count" min="1" value="1" class ="txtMult form-control" type="number" name="txtEmmail" /> 
    <input type="text" value="250" class ="txtMult" name="txtEmmail"/> 
    <span class="multTotal">250</span> 

<select class="selectpicker"> 
<option value="1">multiply with 1</option> 
<option value="2">multiply with 2</option> 
</select> 

<span id="grandTotal">250</span> 

而這裏的Javascript:

$(function(){ 
       $('input').change(function(){ 
        var p=$(this).parent().parent() 
        var m=p.find('input.txtMult') 
        var mul=parseInt($(m[0]).val()*$(m[1]).val()).toFixed(2) 
        var res=p.find('.multTotal') 
        res.html(mul); 
        var total=0; 
        $('.multTotal').each(function(){ 
         total+=parseInt($(this).html()) 
        }) 
        $('#grandTotal').html(parseInt(total).toFixed(2)) 
       }); 
      }) 


$(window).load(function(){ 
$('select').change(function() { 
    $("#grandTotal").html($('select').val()*$('.multTotal').html()) 
}); 
}); 
</script> 

所以我祝願#grandTotal總是在不斷變化的權利。如果選擇例如「乘以2」,並且在改變我的「#count」的值之後,「#grandTotal」應該自動顯示正確的數字。我對計算很困惑。也許你會看得更清楚。非常感謝

+0

什麼是錯誤的計算或更改事件不起作用。 – Anil

回答

1

工作撥弄 http://jsfiddle.net/anilk/6fLk8w8a/1/

更新JS

$(function(){ 
       $('.txtMult').change(function(){ 
        alert("work"); 
        var p=$(this).parent().parent() 
        var m=p.find('input.txtMult') 
        var mul=parseInt($(m[0]).val()*$(m[1]).val()).toFixed(2) 
        var res=p.find('.multTotal') 
        res.html(mul); 
        var total=0; 
        $('.multTotal').each(function(){ 
         total+=parseInt($(this).html()); 
        }) 
        alert(parseInt(total).toFixed(2)); 
        $('#grandTotal').html(parseInt(total).toFixed(2)); 
       }); 
      }) 



$('.selectpicker').change(function() { 
    calcVal(); 
}); 

function calcVal(){ 
    alert($('select').val()*$('.multTotal').html()); 
    $("#grandTotal").html($('select').val()*$('.multTotal').html()) 
} 

// call on document load 
calcVal(); 

你的綁定不正確,用正確的類。

+0

非常感謝!這工作得很好! – fala

0

雖然在JavaScript方面計算,你必須解析爲int或浮動。

您可以使用parseIntparseFloat()

var i = parseInt($('select').val()) * parseInt($('.multTotal').html()) 
$("#grandTotal").html(i);