[英]jquery fill form field based on select2
我正在構建一個 B2B 訂購應用程序,並嘗試根據 select2 選擇填寫表單字段。 在桌子上,有一個“添加”按鈕,可以根據需要添加更多行。 因此,當您選擇產品時,(使用 select2 下拉菜單)它應該從數據庫中獲取定價和與產品相關的折扣。 這適用於第一行/行。
任何后續行都不會從數據庫中獲取價格和折扣。
編輯:
澄清一點 - 如果您單擊“添加”按鈕,則會添加多行而不會出錯。 價格獲取腳本在第一行之后不會獲取任何數據。
任何幫助,將不勝感激
添加行代碼:
$(".custom-select").select2();
let row_number = {{ count(old('products', [''])) }};
$("#add_row").click(function(e) {
e.preventDefault();
var options = $("#products_table tbody tr:first").find("[name='StockItem[]']").html();
var cloned = $("#products_table tbody tr:first").clone().show();
$(cloned).find("td:first").html('<select name="StockItem[]" class="select2 form-control mb-3 custom-select" style="width: 100%; height:36px;">' + options + '</select>')
$(cloned).find("input").val("")
$('#products_table tbody').append('<tr id="product' + (row_number + 1) + '">' + $(cloned).html() + '</tr>');
$("select").select2();
row_number++;
});
獲取價格/折扣
$('#product').on('keyup change', function(){
var product_id = $("#product").val();
$.post('/orders/getprice/' + product_id,
function (data) {
$('#UnitPrice').val(data.SellingPrice);
$('#Discount').val(data.DiscountPercentage);
});
})
問題似乎不夠清楚,但我認為您正在討論的是因為var product_id = $("#product").val();
而你在 html 中生成的id="product' + (row_number + 1) + '"
是id="product' + (row_number + 1) + '"
$('#products_table tbody').append('<tr class="product_abc" id="product' + (row_number + 1) + '">' + $(cloned).html() + '</tr>');
$('.product_abc').on('keyup change', function(e){
console.log(e.target); // this will be your button which is clicked.
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.