我有一个带有生成的输入字段和按钮的DataTable。当我单击按钮时,我想在console.log中输入输入字段内的值..有人可以帮忙吗?这是我生成的表格:
var productsTable = $('#productsTable').DataTable({
responsive: true,
"ajax": {
"url": "/api/products/",
"dataSrc": ""
},
"columns": [
{ "title": "Product Code", "data": "product_code" },
{ "title": "Supplier", "data": "supplier" },
{ "title": "Category", "data": "category"},
{ "title": "Description", "data": "description"},
{ "title": "Price", "data": "invoice_price"},
{ "title": "Quantity", "defaultContent": "<input class='quantity' type='number'>" },
{ "title": "", "defaultContent": "<button class='btn btn-primary'>Click Me!</button>" },
]
});
这是我对点击事件的尝试:
$("#productsTable tbody").on('click', 'button', function() {
var quantity = $(this).parents("tr:first").find('.quantity input').val();
console.log(quantity); // returns undefined
});
有人可以帮忙吗?
提前致谢!
答案 0 :(得分:2)
请试试这个
var quantity = $(this).parents("tr:first").find('.quantity').val();