如何在td中插入文本字段值

时间:2015-06-06 12:12:20

标签: jquery html

我需要使用jQuery在表中的textfield中插入值。 我有一个包含三个单元格的表,我使用jQuery函数在我的表上附加元素,但是我无法在td中获得textfield的值。

这是我的表:

<table id="tableCmp" border name="tableCmp" style="width:25%;border-collapse:collapse;">

<tr>
<th width="5%"></th>
<th>Intitule Compagnie</th>     
<th width="15%">Action</th>
</tr>
</table>

这是我的文字字段:

<input type="text" id="text" />

这是我的功能:

function showdata()
 {
 $('#tableCmp').append('<tr><td  style="text-align:center;"><input type="checkbox" name="myTextEditBox" id="myTextEditBox"></td><td>$('#text').val()</td><td><input type="button" value="M" /><input type="button" value="X" /></td></tr>');
 }

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

function showdata()
 {
   $('#tableCmp').append('<tr><td  style="text-align:center;"><input type="checkbox" name="myTextEditBox" id="myTextEditBox"></td><td>' + $("#text").val() +'</td><td><input type="button" value="M" /><input type="button" value="X" /></td></tr>');
 }

答案 1 :(得分:1)

您已在括号内添加了$(&#34; #text&#34;)。val(),请尝试使用此

$('#tableCmp').append('<tr><td  style="text-align:center;"><input type="checkbox" name="myTextEditBox" id="myTextEditBox"></td><td>' + $('#text').val() + '</td><td><input type="button" value="M" /><input type="button" value="X" /></td></tr>');