jQuery - 单击时添加文本框的值并在文本框中显示总计

时间:2014-08-04 19:17:59

标签: jquery

好吧所以我有一组我想要加在一起的值'资产',我减去'扣除'的总价值来获得总价值。

当我单击“计算”时,除非我在Firefox中按F5然后再次按“计算”(然后它以某种方式工作?)

关于为什么这种方法无法正常工作的任何想法?

这是页面: -

http://everday.co.uk/donate/calculator/

和代码: -

 <script>
    jQuery(document).ready(function(){
    /***** NISAB VALUE ******/
    jQuery('#txt-nisab').val('447');
    /************************/
    var assets = parseInt(jQuery('#txt-gold').val()) + parseInt(jQuery('#txt-silver').val()) + parseInt(jQuery('#txt-bank').val()) + parseInt(jQuery('#txt-future').val()) + parseInt(jQuery('#txt-loans').val()) + parseInt(jQuery('#txt-shares').val()) +  parseInt(jQuery('#txt-stock').val());
    var deduct = parseInt(jQuery('#txt-nisab').val()) + parseInt(jQuery('#txt-wages').val()) + parseInt(jQuery('#txt-taxes').val()) + parseInt(jQuery('#txt-borrow').val());
    var zakat = assets - deduct;
    jQuery('#btn-calc').bind('click', function() {
    if(zakat < 0){ zakat = 0 }
    jQuery('#txt-total').val(zakat);
    jQuery('#donation-calulate').show();
    });
    jQuery('#btn-reset').bind('click', function() {
    jQuery('#txt-gold,#txt-silver,#txt-bank,#txt-future,#txt-loans,#txt-shares,#txt-stock,#txt-taxes,#txt-borrow,#txt-wages').val('0');
    jQuery('#donation-calulate').hide();
    });
    });
    </script>

1 个答案:

答案 0 :(得分:0)

以下是与您类似的FIDDLE,可用于比较。

您收到任何错误消息吗?

您应该将基数添加到parseInt - parseInt(var1,10);

JS

var asset1, asset2, asset3, deduction1, deduction2, deduction3, result;

$('#calculate').on('click', function (){
  asset1 = parseInt( $('#a1').val(), 10 );
    if(asset1.length < 1)
    {
     asset1 = 0;
     }
  asset2 = parseInt( $('#a2').val(), 10 );
    if(asset2.length < 1)
    {
     asset2 = 0;
     }
  asset3 = parseInt( $('#a3').val(), 10 );
    if(asset3.length < 1)
    {
     asset3 = 0;
     }
  deduction1 = parseInt( $('#d1').val(), 10 );
    if(deduction1.length < 1)
    {
     deduction1 = 0;
     }
  deduction2 = parseInt( $('#d2').val(), 10 );
    if(deduction2.length < 1)
    {
     deduction2 = 0;
     }
  deduction3 = parseInt( $('#d3').val(), 10 );
    if(deduction3.length < 1)
    {
     deduction3 = 0;
     }
 result2 = (asset1 + asset2 + asset3) - (deduction1 + deduction2 + deduction3);
    console.log(result2);
    $('.result').text("Suggested Donation: " + result2.toFixed(0));
});