i need to insert value from textfield in a table , with jQuery . I have a table with three cells , and i use a jQuery function to append element on my table , but i can't get the value of my textfield in the td .
我需要使用jQuery从表中的textfield插入值。我有一个包含三个单元格的表,并且我使用jQuery函数在我的表上追加元素,但是我无法在td中获取textfield的值。
this is my table:
这是我的表:
<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>
this is my text field:
这是我的文字字段:
<input type="text" id="text" />
this is my function:
这是我的功能:
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 个解决方案
#1
1
You can try this:
你可以试试这个:
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
1
You have added $("#text").val() inside of the brackets, Try using this
您已在括号内添加了$(“#text”)。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>');
#1
1
You can try this:
你可以试试这个:
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
1
You have added $("#text").val() inside of the brackets, Try using this
您已在括号内添加了$(“#text”)。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>');