我动态创建了一个包含三列的表。
<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。
我怎样才能让它为我带来价值?
答案 0 :(得分: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>
这是一个小提琴,可以帮助你:
答案 1 :(得分:1)
假设#codeSegmentBody
是您的表名,请尝试以下方法:
$("#codeSegmentBody td input").each(function() {
var inputValue = $(this).val();
alert(inputValue);
});
答案 2 :(得分:1)
$("#codeSegmentBody tr input[type='text']").each(function(){
alert($(this).val());
})
答案 3 :(得分:1)
试试这个
$("#codeSegmentBody tr").each(function() {
alert($(this).find("input").val());
});
答案 4 :(得分:1)
您正在引用包含<td>
而不是输入。尝试:
$("#codeSegmentBody").children().eq(x).find(":text").val();
答案 5 :(得分:0)
var str = "";
$("#codeSegmentBody .third-column input").each(function()
{
str += this.value;
});
alert(str);
答案 6 :(得分:0)
不容易
$("table tr td input").val();
顺便说一句。无论如何,此输入中没有任何值。