这是我的问题。
我有这个 html 表格:
<table class="flexme" id="table" border="1">
<thead>
<tr>
<th width="100">Usuario</th>
<th width="100">Nombre</th>
<th width="100">Apellido</th>
<th width="100">Cedula/Rif</th>
<th width="140">Direccion</th>
<th width="100">E-mail</th>
<th width="100">Telefono1</th>
<th width="100">Telefono2</th>
<th width="100">Status</th>
<th width="150">Acción</th>
</tr>
</thead>
<tbody>
<tr id="test">
<td></td>
</tr>
</tbody>
</table>
我有这个 ajax 请求:
$.ajax({
type : "POST",
url : "service.php",
dataType: "json",
data: {
action:"search",
type: 'users',
parameter: parameter,
parameterContent: parameterContent,
},
success:function(data) {
$('#searchResults').show();
var len = data.length;
for (var i = 0; i< len; i++) {
var username = data[i].username;
var name = data[i].uname;
var lastname = data[i].lastname;
}
})
用来自 JSON 的信息填充 html 表的正确方法是什么?我一直在尝试但没有成功。我已经使用 append()
html()
进行了测试,但完全没有成功,有人可以指出我正确的方向吗?
我想要的是获取来自 JSON 的信息并使用此信息动态填充表格。
最佳答案
你可以试试这个:
var table = $("#table tbody");
$.each(data, function(idx, elem){
table.append("<tr><td>"+elem.username+"</td><td>"+elem.name+"</td> <td>"+elem.lastname+"</td></tr>");
});
更多信息可以在这里找到: http://api.jquery.com/jQuery.each/
关于jquery - 在 jQuery 成功事件上填充 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12048922/