2012-01-13 41 views
2

我動態地創建了一個包含三列的表格。試圖從文本框中的列中獲取值

<tr> 
    <td>1</td> 
    <td>SegName</td> 
    <td><input type='text' /></td> 
</tr> 

我想寫一個函數,通過每一行,並抓住將在文本框中的值。

的Javascript:

$("#codeSegmentBody").children().eq(x).children().eq(2).val(); 

代碼帶來了帶來了不確定的,當我做VAL(),但如果我不HTML時,它會抓住文本框的HTML。

我怎樣才能讓我獲得價值?

回答

3
<table id="test"> 
    <tr> 
     <td> 
      <input type="text" value="123"> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <input type="text" value="abc"> 
     </td> 
    </tr> 
</table> 

<script type="text/javascript"> 
$(document).ready(function(){ 

    $("#test").find(":input[type=text]").each(function(){ 

     alert($(this).val()); 

    }); 

}); 
</script> 

這裏是一個小提琴,將讓你有:

http://jsfiddle.net/uS8AK/

0
var str = ""; 
$("#codeSegmentBody .third-column input").each(function() 
{ 
    str += this.value; 
}); 
alert(str); 
0

不容易

$("table tr td input").val(); 

順便說一句。無論如何,你在這個輸入中沒有任何價值。

+0

如果有多行? ;) – 2012-01-13 16:49:22

+1

順便說一句,用戶可以輸入一些東西;) – ComFreek 2012-01-13 16:50:41

1

假設#codeSegmentBody是你的表的名稱,試試這個:

$("#codeSegmentBody td input").each(function() { 
    var inputValue = $(this).val(); 
    alert(inputValue); 
}); 

Example fiddle

1
$("#codeSegmentBody tr input[type='text']").each(function(){ 
    alert($(this).val()); 
}) 
1

試試這個

$("#codeSegmentBody tr").each(function() { 
    alert($(this).find("input").val()); 
}); 
1

你引用的含<td>沒有輸入。請嘗試:

$("#codeSegmentBody").children().eq(x).find(":text").val();