Php 如何使用jQuery填充输入字段

Php 如何使用jQuery填充输入字段,php,jquery,html,Php,Jquery,Html,下面是我的系统截图,它允许我根据发票记录付款。在我的系统中,发票称为租户费用 我有一个标有“已支付总额”的输入,旁边有一个标有“分配”的按钮。当我单击此按钮时,我希望“收到的金额”输入从第一个到最后一个自动填充。例如,如果我在“收到的总金额”输入中输入“200”并按下“分配”,则第一个“收到的金额”字段将填充“189.59”,第二个字段将填充“10.41” HTML 首先,我将把您的表构造为ad和tbody,以实现更高效的循环,并为其添加一个独特的类名,并为每行添加一个具有适当总和的隐藏输入。这

下面是我的系统截图,它允许我根据发票记录付款。在我的系统中,发票称为租户费用

我有一个标有“已支付总额”的输入,旁边有一个标有“分配”的按钮。当我单击此按钮时,我希望“收到的金额”输入从第一个到最后一个自动填充。例如,如果我在“收到的总金额”输入中输入“200”并按下“分配”,则第一个“收到的金额”字段将填充“189.59”,第二个字段将填充“10.41”

HTML


首先,我将把您的表构造为ad和tbody,以实现更高效的循环,并为其添加一个独特的类名,并为每行添加一个具有适当总和的隐藏输入。这里有一个基本的例子

var sum=parseFloat$“支付的总金额”。val; $'.tenants tbody tr'.eachfunction{ var due=parseFloat$'.amount_due',this.val; var金额=总额>=到期?到期:总额; $'.amount_received',this.valamount; 总和-=金额; }; ... 应付金额 收到的金额 ... ... &磅;123.45 ... ...
在循环中,从上一列中获取金额,然后从总数中减去

$( "#allocate_total_amount_paid" ).click(function() {
    var totalAmountPaid = parseFloat($("#total_amount_paid").val());
    $( ".amount_received" ).each(function( index ) {
        var thisAmount = parseFloat($(this).closest("td").prev("td").find("input[name^=amount_outstanding]").val());
        if (thisAmount <= totalAmountPaid) {
            // If we have enough for this payment, pay it in full
            $(this).val(thisAmount);
            // and then subtract from the total payment
            totalAmountPaid -= thisAmount;
        } else {
            // We don't have enough, so just pay what we have available
            $(this).val(totalAmountPaid);
            // Now we have nothing left, use 0 for remaining rows
            totalAmountPaid = 0;
        }
    });

});

到目前为止您尝试了什么?@Blazemonger-我知道如何使用jQuery设置输入字段的值。我不明白的是怎么做的逻辑。我会想象某种类型的循环?我目前还没有尝试任何代码。javascript在哪里?您尝试过什么吗?您可以通过向按钮添加事件处理程序来触发每个输入更新。@Blazemonger-是的,我理解,我需要的帮助是在按下“分配”按钮时运行的实际代码。我可以想象,逻辑就像是在每个“收到的金额”字段中循环,直到没有剩余的资金。我不知道如何在jQuery中做到这一点,因此我请求帮助。这行代码是做什么的?var thisAmount=parseFloat$this.closesttd.prevtd.findinput[name^=amount\u未决].val;我们可以用字段的“max”属性来代替此行吗?var thismount=$this.attr'max';是的,你可以。我没有注意到HTML中的属性。$this.closesttd获取包含循环中当前输入的单元格。prevtd在它之前获取单元格。findinput[name^=amount\u Understand]获取名称以该单元格中amount\u Understand开头的输入。这其中哪一部分很难理解?
// allocate button

$( "#allocate_total_amount_paid" ).click(function() {

var totalAmountPaid = $("#total_amount_paid").val();

$( ".amount_received" ).each(function( index ) {
  //console.log( index + ": " + $( this ).text() );
  alert(index + totalAmountPaid);
});

});
$( "#allocate_total_amount_paid" ).click(function() {
    var totalAmountPaid = parseFloat($("#total_amount_paid").val());
    $( ".amount_received" ).each(function( index ) {
        var thisAmount = parseFloat($(this).closest("td").prev("td").find("input[name^=amount_outstanding]").val());
        if (thisAmount <= totalAmountPaid) {
            // If we have enough for this payment, pay it in full
            $(this).val(thisAmount);
            // and then subtract from the total payment
            totalAmountPaid -= thisAmount;
        } else {
            // We don't have enough, so just pay what we have available
            $(this).val(totalAmountPaid);
            // Now we have nothing left, use 0 for remaining rows
            totalAmountPaid = 0;
        }
    });

});