繁体   English   中英

如果输入随jQuery更改事件而更改,则重置总和

[英]Reset total sum if input changes with jquery change event

我有一个输入列表,我将使用它们来设置值(0到100)。 这些输入的总和不能超过100,我已经完成了并且可以正常工作,但是如果我更改其中一个输入并且总和小于100,我需要一种方法减去总和

这是到目前为止我所拥有的一个例子:

var soma_total = 0; 
jQuery(function($){
    $('input[type=text]').each(function () {
        $(this).change(function(){ 

        var valor = $(this).val();
        valorPorcentagem = valor;   
            eE = procPorcentagem(valorPorcentagem);

            eE = Math.ceil(eE);

            valorPorcentagem = parseInt(valorPorcentagem);

            if((parseInt(valorPorcentagem) + soma_total) > 100)
                valorPorcentagem = 100 - soma_total;
                $(this).val(valorPorcentagem);
                soma_total += valorPorcentagem;
console.log(soma_total);
                $('#final_sum').append('<li>'+soma_total+'</li>');

        });
    });
});

function procPorcentagem(entradaUsuario){
var resultadoPorcem = (entradaUsuario * 48) / 100;
return resultadoPorcem;
}

的jsfiddle

请帮助,谢谢!

该演示可能会给您一个有关如何进行的想法:

 $(function() { $(':text').val(0).on('change', function(e) { var i = $(':text'), total = 0; i.each(function() { total += +this.value; }); if( total > 100 ) { this.value -= (total - 100); } total = 0; i.each(function() { total += +this.value; }); $('#final_sum').text( total ); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text"> <input type="text"> <input type="text"> <div id="final_sum">0</div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM