如何使用jQuery填充输入字段

时间:2015-12-18 16:36:36

标签: php jquery html

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

enter image description here

我的输入标有'支付的总金额'旁边有一个标有' Allocate'的按钮。当我点击此按钮时,我想要收到的金额'从第一个到最后一个自动填充的输入。例如,如果我输入了' 200'收到的总金额'输入并按下'分配',它将填充第一个“已收到的金额”'字段用' 189.59'第二个是' 10.41'。

HTML;

    <fieldset>
    <legend>Outstanding Tenant Charge Details</legend>
<div style="padding:5px;"><label for="total_amount_paid">Total Amount Paid (&pound;):</label>
    <input type="number" id="total_amount_paid" value="0.00">&nbsp;<button type="button" id="allocate_total_amount_paid">Allocate</button></div>
<table class="solid" style="margin:5px;"><tr>
        <th>Tenant Charge #</th>
        <th>Date</th>
        <th>Due Date</th>
        <th>Charge Total</th>
        <th>Amount Paid</th>
        <th>Amount Due</th>
        <th>Amount Received (&pound;)</th><th>Management Fee at 7.00%</th></tr><tr>
        <td><a href="view_tenant_charge.php?tenant_charge_id=217" target="_blank">217</a></td>
        <td>09/11/15</td>
        <td>09/12/15</td>
        <td>&pound;250.00</td>
        <td>&pound;60.41</td>
        <td>&pound;189.59
        <input type="hidden" name="amount_outstanding[]" value="189.59">
        </td>
        <td>
        <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="189.59" required>&nbsp;<button class="pay_in_full" type="button">Pay in Full</button>
        <input type="hidden" name="tenant_charge_id[]" value="217">
        <input type="hidden" name="tenant_charge_tenancy_id[]" value="69"></td><td><input type="checkbox" name="management_fee_invoice[]" value="1"checked="checked"> <label>Generate &amp; Post Invoice</label></td></tr><tr>
        <td><a href="view_tenant_charge.php?tenant_charge_id=283" target="_blank">283</a></td>
        <td>09/12/15</td>
        <td>09/01/16</td>
        <td>&pound;250.00</td>
        <td>&pound;0.00</td>
        <td>&pound;250.00
        <input type="hidden" name="amount_outstanding[]" value="250.00">
        </td>
        <td>
        <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="250.00" required>&nbsp;<button class="pay_in_full" type="button">Pay in Full</button>
        <input type="hidden" name="tenant_charge_id[]" value="283">
        <input type="hidden" name="tenant_charge_tenancy_id[]" value="69"></td><td><input type="checkbox" name="management_fee_invoice[]" value="1"checked="checked"> <label>Generate &amp; Post Invoice</label></td></tr></table></fieldset>

jQuery的;

// 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);
});

});

2 个答案:

答案 0 :(得分:1)

首先,我将您的表格结构化为theadtbody以获得更有效的循环,并为其添加一个独特的类名,并为每一行添加一个带有应有总和的隐藏输入。这是一个基本的例子。

var sum = parseFloat($('#total_amount_paid').val());
$('.tenants tbody tr').each(function() {
  var due = parseFloat($('.amount_due', this).val());
  var amount = (sum >= due) ? due : sum;
  $('.amount_received', this).val(amount);
  sum -= amount;
});
<table class="tenants">
  <thead>
    <tr>
      <th>...</th>
      <th>Amount due</th>
      <th>Amount received</th>
      <th>...</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>...</td>
      <td>
        &pound;123.45
        <input type="hidden" class="amount_due" value="123.45" />
      </td>
      <td>
        <input class="amount_received" />
      </td>
      <td>...</td>
    </tr>
    ...
  </tbody>
</table>

答案 1 :(得分:1)

在你的循环中,从前一列中获取金额,然后从总计中减去它。

$( "#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;
        }
    });

});