2016-07-05 25 views
0

我有一個帶有生成輸入字段和按鈕的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 
}); 

有人能幫忙嗎?

在此先感謝!

回答

2

試試這個請

var quantity = $(this).parents("tr:first").find('.quantity').val(); 
+0

行之有效!我還有一個類似的問題 - 當我點擊按鈕時,我也需要返回'product_code'。 'var product_code = productsTable.row($(this).parents('tr'))。data()。product_code;'當表格爲全尺寸時工作,但在表格處於響應模式時不工作...你有什麼想法我可以在響應模式下返回'product_code'嗎?提前致謝! –

+0

@ user1547174 mmm其實不是,我需要看文檔! – MoustafaS