![](/img/trans.png)
[英]Autocompletion on dynamically created input fields using jQuery and Typeahead
[英]Change the value of dynamically created input fields using jQuery
請查看以下jsfiddle;
https://jsfiddle.net/1kvwxmyb/3/
基本上,代碼允許用戶將“行”添加到發票中。 當用戶選擇產品的“增值稅稅率”時,我需要使用jQuery計算“增值稅金額”和“總金額”。 計算這些數字是基本的數學,但目前我甚至無法讓jQuery在“VAT Amount”和“Gross Amount”中設置任何類型的值,更不用說正確的數字了!
請幫忙。
jQuery的
// calculate VAT amount and set gross amount
$(".vat_rate").change(function () {
alert("Handler for .change() called.");
$(this).nextAll('input').first().val("123");
});
因此,首先,將有問題的代碼更改為以下內容。
// calculate VAT amount and set gross amount
$(".nobord").on("change", ".vat_rate", function () {
var $row = $(this).closest("tr");
var $qty = $row.find("input[name^='invoice_line_quantity']");
var $amount = $row.find("input[name^='invoice_line_amount']");
//Value of the Qty input
console.log($qty.val());
//Value of the Net Amount input
console.log($amount.val());
//Now do your math and set the value as below
$row.find("input[name^='invoice_line_vat_amount']").val("your_value");
$row.find("input[name^='invoice_line_gross_amount']").val("your_value");
});
由於關閉TD / TR的順序不正確,你的HTML有點搞砸了。 看看我對這個小提琴的修正。
希望這是一個好的開始:)
我更新了你的小提琴https://jsfiddle.net/1kvwxmyb/9/
剛剛為clone
函數添加了true
參數,這會導致帶有事件處理程序的clone元素。
來自jQuery文檔的引用
.clone([withDataAndEvents])
withDataAndEvents(默認值:false)
類型:布爾值
一個布爾值,指示是否應將事件處理程序與元素一起復制。 從jQuery 1.4開始,元素數據也將被復制。
我還添加了樣本計算來change
功能。
獲取選項更改的var $row = $(this).closest("tr");
然后你可以用$row.find("input[name^='invoice_line_vat_amount']")
找到輸入$row.find("input[name^='invoice_line_vat_amount']")
修復了,現在它適用於新行https://jsfiddle.net/1kvwxmyb/11/
JS
$(document).on('change', '.vat_rate',function () {
var row = $(this).parent().parent().attr("id");
var qty = $("#" + row + " td:nth-child(1) input");
var desc = $("#" + row + " td:nth-child(2) input");
var netam = $("#" + row + " td:nth-child(3) input");
var vatam = $("#" + row + " td:nth-child(5) input");
var grossam = $("#" + row + " td:nth-child(6) input");
vatam.val("123");
//alert("Handler for .change() called.");
//$(this).nextAll('input').first().val("123");
});
一點變化
// calculate VAT amount and set gross amount
$(".vat_rate").change(function (event) {
$(event.target).parent().parent().find('td:nth-child(3) input').val('0.5');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.