2016-01-14 48 views
3

當我點擊'生成'按鈕時,我想按代碼生成#payment_reference字段值。我認爲該代碼使得它非常清楚我想要它做的事,但由於某種原因,它不會工作:(爲什麼jQuery'最接近'的代碼不起作用?

我添加了用於測試目的的alert,但它只是輸出undefined

$(document).ready(function() { 
 
// generate payment reference 
 

 
$("#generate_payment_reference").click(function() { 
 
    $(".amount_received").each(function(index) { 
 
\t \t var thisAmount = parseFloat($(this).val()); 
 
\t \t var paymentRef = ''; 
 
     if (thisAmount > 0) { 
 
      paymentRef += $(this).closest(".invoice_reference").val(); 
 
\t \t \t alert($(this).closest(".invoice_reference").val()); 
 
     } 
 
\t \t $('#payment_reference').val(paymentRef); 
 
    }); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 
<fieldset> 
 
    <legend>Client Details</legend> 
 

 
    <table class="nobord"> 
 
    <tr> 
 
     <td>Type:</td> 
 
     <td>Landlord </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Name:</td> 
 
     <td>Mr XXX</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Client Bank Account Balance:</td> 
 
     <td>&pound;387.68</td> 
 
    </tr> 
 
    </table> 
 

 
</fieldset> 
 
<fieldset> 
 
    <legend>Outstanding Invoice Details</legend> 
 
    <table class="solid" style="margin:5px;"> 
 
    <tr> 
 
     <th>Invoice #</th> 
 
     <th>Date</th> 
 
     <th>Due Date</th> 
 
     <th>Invoice Total</th> 
 
     <th>Amount Paid</th> 
 
     <th>Amount Due</th> 
 
     <th>Amount Received (&pound;)</th> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="view_invoice.php?invoice_id=496" target="_blank">496</a> 
 
     <input type="text" class="invoice_reference" value="496"> 
 
     </td> 
 
     <td>14/01/16</td> 
 
     <td>14/01/16</td> 
 
     <td>&pound;25.20</td> 
 
     <td>&pound;0.00</td> 
 
     <td>&pound;25.20 
 
     <input type="hidden" name="amount_outstanding[]" value="25.20"> 
 
     </td> 
 
     <td> 
 
     <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="25.20" required>&nbsp; 
 
     <button class="pay_in_full" type="button">Pay in Full</button> 
 
     <input type="hidden" name="invoice_id[]" value="496"> 
 
     <input type="hidden" name="invoice_tenancy_id[]" value="40"> 
 
     <input type="hidden" name="invoice_property_id[]" value="119"> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td><a href="view_invoice.php?invoice_id=497" target="_blank">497</a> 
 
     <input type="text" class="invoice_reference" value="497"> 
 
     </td> 
 
     <td>14/01/16</td> 
 
     <td>14/01/16</td> 
 
     <td>&pound;25.20</td> 
 
     <td>&pound;0.00</td> 
 
     <td>&pound;25.20 
 
     <input type="hidden" name="amount_outstanding[]" value="25.20"> 
 
     </td> 
 
     <td> 
 
     <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="25.20" required>&nbsp; 
 
     <button class="pay_in_full" type="button">Pay in Full</button> 
 
     <input type="hidden" name="invoice_id[]" value="497"> 
 
     <input type="hidden" name="invoice_tenancy_id[]" value="40"> 
 
     <input type="hidden" name="invoice_property_id[]" value="119"> 
 
     </td> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
</fieldset> 
 
<fieldset> 
 
    <legend>Payment Details</legend> 
 

 
    <table class="nobord"> 
 
    <tr> 
 
     <td> 
 
     <label for="invoice_payment_date">Date:</label> 
 
     </td> 
 
     <td> 
 
     <input type="date" id="invoice_payment_date" name="invoice_payment_date" class="datepicker2months" ondblclick="this.value='2016-01-14';" readonly required> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
     <label for="total_amount_received">Total Amount Received (&pound;):</label> 
 
     </td> 
 
     <td> 
 
     <input type="number" id="total_amount_received" name="total_amount_received" readonly required> 
 
     <input type="hidden" id="client_bank_account_balance" name="client_bank_account_balance" value="387.68" required> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
     <label for="payment_reference">Payment Reference:</label> 
 
     </td> 
 
     <td> 
 
     <input type="text" id="payment_reference" readonly>&nbsp; 
 
     <button id="generate_payment_reference" type="button">Generate</button> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
     <label for="invoice_payment_use_balance">Pay from Client Bank Account Balance:</label> 
 
     </td> 
 
     <td> 
 
     <select id="invoice_payment_use_balance" name="invoice_payment_use_balance" required> 
 
      <option value="">Please Select</option> 
 
      <option value="0">No</option> 
 
      <option value="1" selected>Yes</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
     <label for="invoice_payment_method">Method:</label> 
 
     </td> 
 
     <td> 
 
     <select id="invoice_payment_method" name="invoice_payment_method" required> 
 
      <option value="">Please Select</option> 
 
      <option value="2" selected>Bank Transfer</option> 
 
      <option value="1">Cash</option> 
 
      <option value="3">Cheque</option> 
 
      <option value="4">Credit Card</option> 
 
      <option value="5">Debit Card</option> 
 
     </select> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td> 
 
     <label for="invoice_payment_notes">Notes:</label> 
 
     </td> 
 
     <td> 
 
     <textarea id="invoice_payment_notes" name="invoice_payment_notes" rows="6" cols="40"></textarea> 
 
     </td> 
 
    </tr> 
 
    </table> 
 

 
</fieldset>

+1

['.closest()'](https://api.jquery.com/closest/)查找最近的祖先* *元件。 '.invoice_reference'不是一個祖先。你需要做一些額外的[樹遍歷](https://api.jquery.com/category/traversing/tree-traversal/)。 – Blazemonger

+1

如果你整理了HTML(我只是用'Tidy'按鈕在JSFiddle中完成的),層次結構變得更清晰了。 –

回答

4

這是因爲你的.invoice_reference元素是不是你.amount_received元素的祖先。相反,你可以去到tr元素,並使用find().invoice_reference內:

$(this).closest('tr').find(".invoice_reference").val() 
+0

@TrueBlueAussie你知道,在堆棧溢出問題上,我最滿意的答案是在它回答的問題提出後幾乎4年後提交的; 1個半年後,已經提供了300多個upvotes的回答。然而,隨着時間的推移,我的回答大大超過了其他人700票的驚人數字。我在18秒後發佈了這個答案,但直到你在這裏留下你的評論,我甚至知道你的答案存在。時間對於答案的消費來說是一個相當差的判斷。也許人們贊成我的措辭。 –

+0

實際上,對於此類別的問題,您有大量的upvotes數量。也許我需要一張更好的照片:) –

+0

我自己的問題超出了我對健壯性的建議抱怨,所以我刪除了它。這裏有另一個+1。這個網站上的點無論如何都是毫無意義的(雙關意圖):) –

2

如果$(".amount_received").length等於$(".invoice_reference").length,請嘗試使用.eq()與參數index;在.each()之外移動$('#payment_reference').val(paymentRef);以防止覆蓋值。請注意,重複關閉</td> s在html

另請注意,不確定的結果是串接字符串或在paymentRef變量的值的添加?

$(document).ready(function() { 
 
// generate payment reference 
 

 
$("#generate_payment_reference").click(function() { 
 
    var paymentRef = ""; 
 
    $(".amount_received").each(function(index) { 
 
\t \t var thisAmount = parseFloat($(this).val()); 
 
\t \t //var paymentRef = ''; 
 
     if (thisAmount > 0) { 
 
      paymentRef += $(".invoice_reference").eq(index).val(); 
 
     } 
 
\t \t 
 
    }); 
 
    $('#payment_reference').val(paymentRef); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"> 
 
</script> 
 
<fieldset> 
 
<legend>Client Details</legend> 
 

 
<table class="nobord"><tr> 
 
    <td>Type:</td> 
 
\t <td>Landlord </td> 
 
    </tr><tr> 
 
    <td>Name:</td> 
 
\t <td>Mr XXX</td> 
 
    </tr><tr> 
 
    <td>Client Bank Account Balance:</td> 
 
\t <td>&pound;387.68</td> 
 
    </tr></table> 
 

 
</fieldset><fieldset> 
 
<legend>Outstanding Invoice Details</legend><table class="solid" style="margin:5px;"><tr> 
 
    <th>Invoice #</th> 
 
    <th>Date</th> 
 
\t <th>Due Date</th> 
 
    <th>Invoice Total</th> 
 
    <th>Amount Paid</th> 
 
\t <th>Amount Due</th> 
 
\t <th>Amount Received (&pound;)</th> 
 
    </tr><tr> 
 
    <td><a href="view_invoice.php?invoice_id=496" target="_blank">496</a> 
 
\t <input type="text" class="invoice_reference" value="496"> 
 
\t </td> 
 
    <td>14/01/16</td> 
 
\t <td>14/01/16</td> 
 
    <td>&pound;25.20</td> 
 
    <td>&pound;0.00</td> 
 
\t <td>&pound;25.20 
 
\t <input type="hidden" name="amount_outstanding[]" value="25.20"> 
 
\t </td> 
 
\t <td> 
 
\t <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="25.20" required>&nbsp;<button class="pay_in_full" type="button">Pay in Full</button> 
 
\t <input type="hidden" name="invoice_id[]" value="496"> 
 
\t <input type="hidden" name="invoice_tenancy_id[]" value="40"> 
 
\t <input type="hidden" name="invoice_property_id[]" value="119"></td> 
 

 
    </tr> 
 
<tr> 
 
    <td><a href="view_invoice.php?invoice_id=497" target="_blank">497</a> 
 
\t <input type="text" class="invoice_reference" value="497"> 
 
\t </td> 
 
    <td>14/01/16</td> 
 
\t <td>14/01/16</td> 
 
    <td>&pound;25.20</td> 
 
    <td>&pound;0.00</td> 
 
\t <td>&pound;25.20 
 
\t <input type="hidden" name="amount_outstanding[]" value="25.20"> 
 
\t </td> 
 
\t <td> 
 
\t <input type="number" name="amount_received[]" class="amount_received" value="0.00" max="25.20" required>&nbsp;<button class="pay_in_full" type="button">Pay in Full</button> 
 
\t <input type="hidden" name="invoice_id[]" value="497"> 
 
\t <input type="hidden" name="invoice_tenancy_id[]" value="40"> 
 
\t <input type="hidden" name="invoice_property_id[]" value="119"></td> 
 

 
    </tr></table></fieldset> 
 
<fieldset> 
 
<legend>Payment Details</legend> 
 

 
<table class="nobord"><tr> 
 
    <td><label for="invoice_payment_date">Date:</label></td> 
 
\t <td> 
 
\t <input type="date" id="invoice_payment_date" name="invoice_payment_date" class="datepicker2months" ondblclick="this.value='2016-01-14';" readonly required> 
 
\t </td> 
 
    </tr><tr> 
 
    <td><label for="total_amount_received">Total Amount Received (&pound;):</label></td> 
 
\t <td> 
 
\t <input type="number" id="total_amount_received" name="total_amount_received" readonly required> 
 
\t <input type="hidden" id="client_bank_account_balance" name="client_bank_account_balance" value="387.68" required> 
 
\t </td> 
 
    </tr><tr> 
 
    <td><label for="payment_reference">Payment Reference:</label></td> 
 
\t <td> 
 
\t <input type="text" id="payment_reference" readonly>&nbsp;<button id="generate_payment_reference" type="button">Generate</button> 
 
\t </td> 
 
    </tr><tr> 
 
    <td><label for="invoice_payment_use_balance">Pay from Client Bank Account Balance:</label></td> 
 
\t <td><select id="invoice_payment_use_balance" name="invoice_payment_use_balance" required><option value="">Please Select</option><option value="0">No</option><option value="1" selected>Yes</option></select></td></tr><tr> 
 
    <td><label for="invoice_payment_method">Method:</label></td> 
 
\t <td><select id="invoice_payment_method" name="invoice_payment_method" required><option value="">Please Select</option><option value="2" selected>Bank Transfer</option><option value="1">Cash</option><option value="3">Cheque</option><option value="4">Credit Card</option><option value="5">Debit Card</option></select></td></tr><tr> 
 
    <td><label for="invoice_payment_notes">Notes:</label></td> 
 
\t <td><textarea id="invoice_payment_notes" name="invoice_payment_notes" rows="6" cols="40"></textarea></td></tr></table> 
 

 
</fieldset>

+0

這是一種危險的技術,因爲它沒有對'$(「。invoice_reference」)'範圍進行搜索。如果你在這個'fieldset'之外有更多'class =「invoice_reference」'元素,它將會出錯。 –

+0

@TrueBlueAussie查看更新後的帖子,補充說明; '$(「。amount_received」).length'出現等於'$(「。invoice_reference」).length' at'js' – guest271314

+0

它會在這個特定的例子中做,但是如果它們在頁面上有多個表呢?這將重新計算它們。只是我的意見,但範圍搜索更安全的維護。 –

相關問題