简体   繁体   English

使用jQuery基于总数动态地重新计算输入字段值

[英]dynamically recalculate the input field values based on the total amount with jQuery

I have a page which has a Total Amount input field and an HTML table with rows having column showing the item amount(from db) and a input field against it(autopopulates the split amt from the Total Amount and also allows user input). 我有一个页面,该页面具有“总金额”输入字段和一个HTML表,其中的行具有显示项目数量(来自db)的列以及针对该字段的输入字段(自动从“总金额”填充拆分的amt,还允许用户输入)。 My requirement is, when the user inputs the total amount, it should get split into the input boxes(applied amount column) in the table based on their item amount(Item Amount column prepopulated from db) 我的要求是,当用户输入总金额时,应根据他们的项目金额(从db中预先填充的“项目金额”列)将其分成表格中的输入框(“应用金额”列)

Eg: Input Amount:100

Item Amount | Applied amount(input box)
25              25
100             75
50              0

I'm able achieve this by: 我可以通过以下方式实现这一目标:

var oidata = [];
var poidata = "", poibalance = "";
function applyAmount() {
oidata = [];
poidata = "", poibalance = "";
var total = parseFloat(document.getElementById("total-amount").value);
if(total>0){
    $('#ao_amt_tbl tbody tr td[data-value]').each(function() {
    var dataValue =  this.getAttribute("data-value");
    var dataId =  this.getAttribute("data-id");
    var $row = $(this).closest("tr");
    var priceAmt = 0;
    if(dataValue > 0){
        if(total > dataValue || 
            total == dataValue){
                total = total - dataValue;
                $row.find('.applied').val(dataValue);
                $row.find('.balance').val('0.00');
                oidata.push(dataId);
               }
               else{                            
                priceAmt = dataValue - total;
                $row.find('.applied').val(total.toFixed(2));
                $row.find('.balance').val(priceAmt.toFixed(2));
                if(total>0){
                    poibalance=priceAmt;
                    poidata=dataId;
                    oidata.push(dataId);
                }
                total=0;                                                        
                }
              }                 
            });
    if(total>0)
            alert('$' + total.toFixed(2) + ' remaining.');
}

the HTML is- HTML是-

    <table id='ao_amt_tbl'>
    <thead>
    <tr>
    <th>Amount</th><th>Amount Applied</th><th>Balance</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td data-id="19185" data-value="25">$25</td>
    <td class="ao_td_jn"><input type="text" name="applied-amount" id="total1" placeholder="0.00" class="applied"></td>
    <td><input type="text" name="balance-amount" id="balance1" placeholder="0.00" class="balance"></td>
    </tr>
    <tr>
    <td data-id="19186" data-value="100">$100</td>
    <td class="ao_td_jn"><input type="text" name="applied-amount" id="total2" placeholder="0.00" class="applied"></td>
    <td><input type="text" name="balance-amount" id="balance2" placeholder="0.00" class="balance"></td>
    </tr>
    <tr>
    <td data-id="19186" data-value="50">$50</td>
    <td class="ao_td_jn"><input type="text" name="applied-amount" id="total2" placeholder="0.00" class="applied"></td>
    <td><input type="text" name="balance-amount" id="balance2" placeholder="0.00" class="balance"></td>
    </tr>
    </tbody>

Now suppose I adjust the second row 'applied-amount' input value it should recalculate and distribute the 100 dollar among other rows. 现在假设我调整了第二行“应用金额”输入值,它应该重新计算并在其他行中分配100美元。 For example, change the second input value to 50, it should recalculate and distribute the values as 25, 50, 25. and recalculate the balance field as 0, 50, 25. 例如,将第二个输入值更改为50,它应该重新计算并将值分配为25、50、25。然后将余额字段重新计算为0、50、25。

Using the above example; 使用上面的例子; If the Input Amount is 100, the system should recalculate the rest of the Applied Amount input vales based on the balance amount from Input Amount(100) 如果输入金额为100,则系统应基于输入金额(100)的余额重新计算其余的“应用金额”输入值。

Item Amount | Applied amount(input box)
25              25
100             50  <-- user input
50              25  <-- script needs to automatically recalculate and change the rest of the Applied amount input with balance amount (100 - 75 = 25).

Basically, the script needs to loop through the 'Applied amount' input boxes and split the Input Amount(100), from top to bottom until the Input amount is 0. Hope I'm clear with my requirement. 基本上,脚本需要遍历“应用量”输入框,并从上到下拆分输入量(100),直到输入量为0。希望我对我的要求很清楚。 Any help will be greatly appreciated. 任何帮助将不胜感激。 thanks 谢谢

use 采用

$(document).ready(function () {
$('input').keydown(function () {
    applyAmount();
});

}); });

you can also use 你也可以使用

onkeydown=applyAmount();

inside the tag of whichever input tag you want. 在您想要的任何输入标签的标签内。

thankyou all.. I got the solution. 谢谢大家。我得到了解决方案。 Did something like this; 做了这样的事情;

 $(function (){
$("input[id*='total-']").on('keyup', function() {       
    var ttl = //get Input Amount
    var gid = $(this).parents('tr').find('.ao_td_jn').attr('data-id');
    var Ajt = this.value;
    ttl = ttl - Ajt;
    if(ttl>0){
        $('#ao_amt_tbl tr td[data-value]').each(function() {
        var dv =  this.getAttribute("data-value");
        var dataId =  this.getAttribute("data-id");
        var $row = $(this).closest("tr");           
        if(gid == dataId){
        var balAmt = dv - Ajt;
        $row.find('.balance').val(balAmt.toFixed(2));
        }
        else{
        ...
        }
        ...
        }
    }
 });

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

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