简体   繁体   English

平衡jQuery中的两个输入数字字段

[英]balancing two input number fields in jquery

I would like to balance two input number fields using jquery based on the max value set for both. 我想基于两者的最大值设置使用jquery平衡两个输入数字字段。 for example its like a balance, if one side goes down the other goes up and vice versa. 例如,它就像一个天平,如果一侧下降,另一侧上升,反之亦然。 another example if the max value is 20 then if i enter 5 in input field one then 15 would be left in input field two. 另一个示例,如果最大值为20,则如果我在输入字段1中输入5,则将在输入字段2中保留15。 Need the help Thanks. 需要帮助谢谢。 Haven't started coding it as yet stuck trying to figure it out. 尚未开始编写代码,但仍想找出答案。

here's a fiddle to get you started (and maybe finished): 这是一个使您开始(也许完成)的小提琴:

https://jsfiddle.net/ahmadabdul3/xwyrrw53/1/ https://jsfiddle.net/ahmadabdul3/xwyrrw53/1/

html: 的HTML:

<input type='number' id='first' class='balancable'/>
<input type='number' id='second' class='balancable'/>

js: js:

$(function() {
    var max = 20;
    var balanceOpposite = {
      'first': 'second',
      'second': 'first',
    }
    $('.balancable').on('input', function() {
      var id = $(this).attr('id');
      var thisVal = $(this).val();
      $('#' + balanceOpposite[id]).val(20 - thisVal);
    });
});

First you need to attach the input eventhandler on all of the relevant input fields. 首先,您需要在所有相关输入字段上附加输入事件处理程序。 This event handler will compare the current input value of a input fields to the total/max value variable and find the remainder accordingly. 此事件处理程序会将输入字段的当前输入值与total / max值变量进行比较,并相应地找到其余部分。 The event handler then finds the other input fields and assigns them with the appropriate remainder values. 然后,事件处理程序找到其他输入字段,并为它们分配适当的余数值。

Note : This allows you to add as many inputs as you want and it will balance them all out. 注意 :这使您可以添加任意数量的输入,并且可以平衡所有输入。 Just remember to add the balance class on the input field. 只需记住在输入字段上添加balance类即可。

 var total = 20; $('.balance').on('input', function() { var value = parseInt(this.value); if (isNaN(value)) { this.value = value = 0; } else if (value > total) { this.value = value = total; }/* else if (value < 0) { this.value = value = 0; } * Remove this comment if value shouldn't be negative. */ var remainder = total - value; var otherInputs = $('.balance'); otherInputs.splice($.inArray(this,otherInputs),1); var remainderDiv = remainder/otherInputs.length; $.each(otherInputs, function(input) { otherInputs[input].value = remainderDiv; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" class="balance"> <input type="number" class="balance"> 

Update : The two inputs can be less than the max but never higher. 更新 :这两个输入可以小于最大值,但决不能更高。

 var max = 20; $('.balance').on('input', function() { var value = parseInt(this.value); if (isNaN(value)) { value = 0; } var otherInputs = $('.balance'); var sum = 0; $.each(otherInputs, function(input) { sum += parseInt(otherInputs[input].value); }); if (sum > max) this.value = max - (sum - value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" class="balance"> <input type="number" class="balance"> 

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

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