2017-03-17 15 views
1

一個文本框的訪問值在數據表中,我通過添加一個文本框列(第2列)追加jQuery的:在數據表

var textbox= '<input type="text" class="txtBox">'; 

但現在,我想要得到的文本框的值。我通過這樣做:

var row_index; 

$(document).on('mouseover', '#table1 tr', function() { 
    row_index = this.rowIndex; 
}); 

    function getIncrement() { 

    var dtable = $('#table1').DataTable(); 
    var textvalue = dtable.rows(row_index).cells(1).value; //textbox column is 2nd 
    alert(parseFloat(textvalue)); 

    } 

問題是我得到一個「男」(非數字)值。如果我刪除parseFloat,我會得到'undefined'。有任何想法嗎?先謝謝你。

P.S. row_index值很好。如果我使用alert來獲取它的值,我會得到正確的索引。此外,我沒有問題使用索引獲取其他行值的值。我只有「txtbox」列有問題。謝謝

+0

[從動態生成的數據表獲得輸入值]的可能的複製(http://stackoverflow.com/questions/28464914/getting-value-from-a-dynamically-generated-datatable-input) – Anil

+0

而不是** dtable.rows(row_index).cells(1).value **您必須使用具有指定類 –

回答

1

您可以存儲row而不是rowIndex,稍後使用find()找到其中的文本框。一旦你得到元素電話val()來獲得它的價值。

$(document).on('mouseover', '#table1 tr', function() { 
    current_row = this; 
}); 

function getIncrement() { 
    alert(parseFloat($(current_row).find(".txtBox").val())); 
} 
+0

的DOM遍歷find()函數非常感謝。這對我:) :):* – lulutanseco

+0

不客氣。 – Adil