ajax响应仅显示jquery和php中数据库的最后一行?
我正在与一些公司合作开发php和jquery。我 要显示特定公司的所有员工,请选择该公司 公司。为此,我在jquery中使用ajax响应。我的回答 向我显示完美的数据,但我的每个函数都无法正常工作。 它只显示数据库中的最后一条记录 ajax响应后将调用的代码:ajax响应仅显示jquery和php中数据库的最后一行?,php,jquery,ajax,Php,Jquery,Ajax,我正在与一些公司合作开发php和jquery。我 要显示特定公司的所有员工,请选择该公司 公司。为此,我在jquery中使用ajax响应。我的回答 向我显示完美的数据,但我的每个函数都无法正常工作。 它只显示数据库中的最后一条记录 ajax响应后将调用的代码: <table class="tabledata"> <thead> <tr>
<table class="tabledata">
<thead>
<tr>
<th>Employee Name</th>
<th>Attach Payslip</th>
</tr>
</thead>
<tbody class="bodytable">
</tbody>
</table>
员工姓名
附上工资单
您应该在每个循环之外清空您的表。像这样:
$.ajax({
method: "GET",
dataType: 'json',
url:"getdata.php?id="+emp_id,
success:function (response){
$(".bodytable").empty();
$.each(response, function( index, value ) {
$("table.table").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
});
},
});
$.ajax({
方法:“获取”,
数据类型:“json”,
url:“getdata.php?id=“+emp_id,
成功:功能(响应){
$(“.bodytable”).empty();
$.each(响应、函数(索引、值){
$(“table.table”).append(“+value.emp_name++”);
});
},
});
此外,您还将直接附加到表。我更喜欢在tbody中添加如下内容:
$(".bodytable").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
$(“.bodytable”).append(“+value.emp_name++”);
希望它能帮助您。显示您的php代码。返回的值是对象,而不是数组。
$(.bodytable”).empty()
在每次迭代时都为空,所以把它放在循环之外。它对我有用。但我必须选择那家公司两次。在第二个选项中,我看到了2名员工。你能添加完整的HTML吗?你能更新你的jquery代码吗?当您获得select的更改事件时添加代码。因为所有其他代码看起来都很好。
$.ajax({
method: "GET",
dataType: 'json',
url:"getdata.php?id="+emp_id,
success:function (response){
$(".bodytable").empty();
$.each(response, function( index, value ) {
$("table.table").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");
});
},
});
$(".bodytable").append("<tr><td>" + value.emp_name + "</td><td>" + "</td><td><input type='file'></td></tr>");